c# - How do I exclude the default named column having no data in my excelsheet which I am fetching in a dataset -
i having excel sheet multiple rows , columns fetching in dataset in .net application(using c#). when see returned dataset through simple select query, excel sheet having required columns want , there blank columns in between them default given name f1, f2, f3 , on. want remove these columns , required columns want. in advance.
try datatable , dataview:
datatable oldtable = dataset.tables[0]; datatable newtable = oldtable.defaultview.totable(false, "only columns want", "only columns want");
with columns names dynamic might try this:
for (int col = datatable.columns.count - 1; col >= 0; col--) { bool removecolumn = true; foreach (datarow row in datatable.rows) { if (!row.isnull(col)) { removecolumn = false; break; } } if (removecolumn) datatable.columns.removeat(col); }
or shorter linq version:
for (var col = datatable.columns.count - 1; col >= 0; col--) { bool removecolumn = datatable.rows.cast<datarow>().all(row => row.isnull(col)); if (removecolumn) datatable.columns.removeat(col); }
Comments
Post a Comment