Wonder of DataGrid imort from Excel file
-
Has a Excel file like that: Title Field1 Field2 Filed3 1 AA 1.1 2 3a CC 3.3 4 DD 4.4 I wrote a program in order to import the excel file datas to DataGrid:
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Test.xls; Extended Properties=Excel 8.0"); connection.Open(); DataTable dt = new DataTable("Orders"); OleDbDataAdapter da = new OleDbDataAdapter(); da.SelectCommand = command; da.Fill(dt); this.dataGridTest.DataSource = dt;
Why in the DataGrid show as: F1 Title F2 ------> This is header (null) Field2 Filed3 1 AA 1.1 2 (null) (null) (null) CC 3.3 ------> Note this line column 1 is null! 4 DD 4.4 If the Excel file like this: Title Field1 Field2 Filed3 1 AA 1.1 2a 3a CC 3.3 4a DD 4.4 That the DataGrid turns to like this: F1 Title F2 ------> This is header Field1 Field2 Filed3 ------> Note the Field1 now is not null (null) AA 1.1 ------> Note this line column 1 is null! 2a (null) (null) 3a CC 3.3 4a DD -
Has a Excel file like that: Title Field1 Field2 Filed3 1 AA 1.1 2 3a CC 3.3 4 DD 4.4 I wrote a program in order to import the excel file datas to DataGrid:
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Test.xls; Extended Properties=Excel 8.0"); connection.Open(); DataTable dt = new DataTable("Orders"); OleDbDataAdapter da = new OleDbDataAdapter(); da.SelectCommand = command; da.Fill(dt); this.dataGridTest.DataSource = dt;
Why in the DataGrid show as: F1 Title F2 ------> This is header (null) Field2 Filed3 1 AA 1.1 2 (null) (null) (null) CC 3.3 ------> Note this line column 1 is null! 4 DD 4.4 If the Excel file like this: Title Field1 Field2 Filed3 1 AA 1.1 2a 3a CC 3.3 4a DD 4.4 That the DataGrid turns to like this: F1 Title F2 ------> This is header Field1 Field2 Filed3 ------> Note the Field1 now is not null (null) AA 1.1 ------> Note this line column 1 is null! 2a (null) (null) 3a CC 3.3 4a DDHi Libra, Can you try something with your command object. Insted of selecting a sheet can you try selecting a range of cells. command.CommandText = "Select * from [Sheet1$A2:B7]"; Thanks and Regards Pani.
-
Hi Libra, Can you try something with your command object. Insted of selecting a sheet can you try selecting a range of cells. command.CommandText = "Select * from [Sheet1$A2:B7]"; Thanks and Regards Pani.
Thx for you help. I try as your said but it's trashy.:sigh:Select of a part datas is base on the Select of whole sheet. I wonder if the OleDb Engineer adjust the type of each excel column when transfer, so when the column looks has more numerical value that it make the numerical type, when the column looks has more string value that it make the string type.:~ === Game is power! ===