我想導入excel文件並將其加載到我的datagridview1
。導入excel文件並顯示DataGridView選中的行到另一個DataGridView
在顯示我的DataGridView
裏面的文件內容後,我想選擇該行並將其傳輸到我的第二個DataGridView
。
請任何人都可以幫助我如何解決我的代碼?因爲我發現了錯誤:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
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);
}
}
請縮進你的代碼。 –