Delete columns from DataTable in C #

I have a DataSet from which I get a DataTable from which I return from a function call. It has 15-20 columns, however I only need 10 columns of data.

Is there a way to delete those columns that I don’t need, copy the DataTable to another one that has only the columns that I want, or is it just better to iterate over the collection and just use the columns you need.

I need to write values ​​to a file with a fixed length.

+100
Sep 16 '08 at 18:00
source share
2 answers

In addition to limiting the columns selected to reduce bandwidth and memory:

DataTable t; t.Columns.Remove("columnName"); t.Columns.RemoveAt(columnIndex); 
+276
Sep 16 '08 at 18:04
source share

To remove all columns after you want, this little function should work. It will be deleted with index 10 (remember that columns are based on 0) until the number of columns is 10 or less.

  DataTable dt; int desiredSize = 10; while (dt.Columns.Count > desiredSize) { dt.Columns.RemoveAt(desiredSize); } 
+21
Sep 16 '08 at 18:38
source share



All Articles