【发布时间】:2016-07-31 01:05:47
【问题描述】:
我正在尝试导入 excel 文件并将其加载到我的datagridview1。
在我的DataGridView 中显示文件的内容后,我想选择该行并将其传输到我的第二个DataGridView。
请任何人都可以帮助我如何修复我的代码?因为我收到错误:
System.Windows.Forms.dll 中出现“System.InvalidOperationException”类型的未处理异常
private void button1_Click(object sender, EventArgs e)
{
string PathConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +textBox1.Text + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection conn = new OleDbConnection(PathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + "Sheet1" + "$]", conn);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox1.Text = openFileDialog1.FileName;
}
}
private void button3_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow selRow in dataGridView1.SelectedRows.OfType<DataGridViewRow>().ToArray())
{
dataGridView2.Rows.Remove(selRow);
dataGridView2.Rows.Add(selRow);
}
}
【问题讨论】:
-
请缩进您的代码。
标签: c# excel winforms datagridview