July 10th, 2013, 02:42 AM
Missing values While importing from excel to datagridview
I have a Winform app that imports an excel file to a datagridview . It is working well but only one issue : I have 2 columns in the excel file that are integer numbers. If I change a value from one of that columns and add a char that isn't an integer , the value is missing in the datagridview.
I have tried to change the column's properties in the excel file to 'Text' as the column type originally is 'Double' , but it still doesn't effects the datagridview.
I also tried to make my own columns with the type I need (all as string) , and then clone the datatable and add the cloned one to the datagridview , but still same problem.
Here is my code when clicked on the import button :
Need help with this , thanks in advance
private void btnImport_Click(object sender, EventArgs e)
#region Import from the Excel
string pathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + txtPath.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";
OleDbConnection conn = new OleDbConnection(pathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + sheetName + "$]", conn);
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataTable dtCloned = dt.Clone();
foreach (DataColumn col in dtCloned.Columns)
col.DataType = typeof(String);
foreach (DataRow row in dt.Rows)
ExceldataGridView.DataSource = dtCloned;
Tags for this Thread
Click Here to Expand Forum to Full Width