Import Excel (.xls) to datagridview

  • Thread starter Thread starter okrus
  • Start date Start date
O

okrus

Guest
Hi,

I know there is multiple solution out there, but none of them work in my case. I have tried debugging, but cant find the problem. I have a Excel (.xls file) named "TI_Testdata" and a datagridview named "testDGV". I want do to it as simple as just importing the Excel file to my DGV with a button click. Any help is strongly appreciated.


This is the code I'm using related to this operation:

delegate void dataToGridTI(int deg, int de2, double ampl_Total, int seq);

//Test DGV - Import TI notations
private void adddataToGridTI(int deg, int deg2, double ampl_Total, int seq)
{
int row = 0;
testDGV.Rows.Add();
row = testDGV.Rows.Count - 2;
testDGV["LIST4", row].Value = seq;
testDGV["Theta4", row].Value = deg;
testDGV["Phi4", row].Value = deg2;
testDGV["Ampl_dB4", row].Value = ampl_Total;

testDGV.FirstDisplayedScrollingRowIndex = testDGV.RowCount - 1;
}

private void fillTestDGV_Btn_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.DataSet DtSet;
System.Data.OleDb.OleDbDataAdapter MyCommand;
MyConnection = new System.Data.OleDb.OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
MyCommand.TableMappings.Add("Table", "Net-informations.com");
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
testDGV.DataSource = DtSet.Tables[0];
MyConnection.Close();
}

Continue reading...
 
Back
Top