-
July 10th, 2013, 02:42 AM
#1
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 :
Code:
private void btnImport_Click(object sender, EventArgs e)
{
#region Import from the Excel
try
{
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();
myDataAdapter.Fill(dt);
System.Data.DataTable dtCloned = dt.Clone();
foreach (DataColumn col in dtCloned.Columns)
{
col.DataType = typeof(String);
}
foreach (DataRow row in dt.Rows)
{
dtCloned.ImportRow(row);
}
ExceldataGridView.DataSource = dtCloned;
dt.Dispose();
myDataAdapter.Dispose();
conn.Close();
}
catch
{
MessageBox.Show("Error");
return;
}
#endregion
}
Need help with this , thanks in advance
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|