-2

I trried to use below code, but it requires Column name or index

DataTable t;
t.Columns.Remove("columnName");
t.Columns.RemoveAt(columnIndex);
Noufal
  • 1
  • 1
  • which line do you want to remove? – Denis Schaf Mar 18 '19 at 14:36
  • if its the only empty one maybe this helps https://stackoverflow.com/questions/1766902/remove-all-columns-with-no-data-from-datatable – Kenan Kundo Mar 18 '19 at 14:40
  • Possible duplicate of [Remove all columns with no data from DataTable](https://stackoverflow.com/questions/1766902/remove-all-columns-with-no-data-from-datatable) – TnTinMn Mar 18 '19 at 16:31

1 Answers1

2

You just need a loop to verify... Something like this:

foreach(var column in t.Columns.Cast<DataColumn>().ToArray()) {
    if (t.AsEnumerable().All(dr => dr.IsNull(column)))
        t.Columns.Remove(column);
}

Credits: here

Mikev
  • 2,012
  • 1
  • 15
  • 27