I want to Compiling data from multiple excel to Datagridview
I'm use CheckListBox for Provider excel Path But not work . dataGridView display data single excel path
Help me please , Thank you.
foreach (var item in checkedListBox1.CheckedItems)
{
string constr = "Provider = Microsoft.ACE.OLEDB.12.0; data source = '" + item + "'" +
@";extended properties=" + "\"excel 8.0;hdr=yes;\"";
string sheetname = "[MySheet$]";
conOle = new OleDbConnection(constr);
comOle = new OleDbCommand("Select * From " + sheetname + "", conOle);
conOle.Open();
datpOle = new OleDbDataAdapter(comOle);
}
dtable = new DataTable();
datpOle.Fill(dtable);
dataGridView2.DataSource = dtable;
Gowtham CpPosted Sep 17, 2024, 9:23 AM
Hello Paradorn,
Glad the solution worked! The issue with cell types is often due to mixed data types in Excel. You can handle this by using the
IMEX=1property in the connection string, which forces all data to be treated as text.Here’s an updated solution:
If you need more fine-grained control over column types, you may need to process the
DataTablefurther after loading the data to adjust the types accordingly.Hope this helps!
Best regards,
Gowtham Cp
ParadornPosted Sep 17, 2024, 2:57 PM
Hello Gowtham Cp
Thanks for all the solutions.
ParadornPosted Sep 17, 2024, 7:31 AM
Hi Gowtham Cp
Thank you for reply
it's work and so great ,
Then I have a problem with the cell type of each file
I am looking for a solution
Gowtham CpPosted Sep 17, 2024, 4:28 AM
Hi Paradorn ,
You need to merge the data from all the Excel files instead of overwriting it each time. Right now, you're re-initializing the
DataTable(dtable) in every loop, which is why only the data from the last file is displayed. The solution is to initialize theDataTableoutside the loop and use theMerge()method to add rows from each file into the sameDataTable. Here’s the code:This way, all selected Excel files will be merged into one
DataTable, and theDataGridViewwill display the combined data.Hope it helps!