DataTable importRow() into empty table
23,507
I assume the reason is because your DataTable currently has no schema. You can try to clone the original DataTable to create the same schema(DataColumns etc).
foreach (DataTable datTab in ds.Tables) // ds is extracted excel sheets in a dataset
{
DataTable tblClone = datTab.Clone();
foreach (DataRow datRow in datTab.Rows)
{
if (datRow.IsNull(0)) //if empty first col go on to next sheet
{
break;
}
else
{
tblClone.ImportRow(datRow);
}
}
}
Author by
Kevin Zhou
Updated on June 16, 2020Comments
-
Kevin Zhou almost 4 years
I've been trying to merge an excel document with many sheets into a Datatable so that I can display said sheet in my winform app.
From reading around, I figured that Datatable.import(DataRow row) is my best bet. Thus my code looks as follows:
DataTable returnSet = new DataTable(); foreach (DataTable datTab in ds.Tables) // ds is extracted excel sheets in a dataset { foreach (DataRow datRow in datTab.Rows) { if (datRow.IsNull(0)) //if empty first col go on to next sheet { break; } else { returnSet.ImportRow(datRow); } } }
When debugging, it shows that datRow/datTab is what I expected it to be, however after each ImportRow, returnSet is still an empty 1x1 cell. Any insight as to what I am doing wrong / missing would be greatly appreciated.