tags:

views:

1312

answers:

3

How can I delete a column of a Dataset?

Is there a method that will do this for me like this:

rh.dsDetail = ds.Tables[0].Columns.Remove(

Or maybe like this:

rh.dsDetail = ds.Tables[0].Columns.Remove( ds.Tables[0].Columns[1],ds.Tables[0].Columns[2])
+4  A: 

First, a DataTable has columns, not a data-set.

If you want to get rid of them, then:

table.Columns.Clear();

otherwise, if you have the index:

table.Columns.RemoveAt(0);

should do the job if you have the column index. Note that if you remove column 0, then the numbers will shuffle (so you might need to do in reverse order).

Alternatively, you may want to remove by name:

table.Columns.Remove("Foo");
Marc Gravell
+2  A: 

There is no such method. But you can simply write your own Helpermethod

public static void RemoveColumns(DataTable dt, params DataColumn[] dc)
{
    foreach (DataColumn c in dc)
    {
        dt.Columns.Remove(c);
    }
}

RemoveColumns(ds.Tables[0], ds.Tables[0].Columns[1], ds.Tables[0].Columns[2]);
michl86
+2  A: 

There is no built in method to remove more than one column at a time. You can create an extension method though if you'd like. Something like this:

 public static class DataColumnCollectionExtensions
    {
        public static void RemoveColumns(this DataColumnCollection column, params string[] columns)
        {
            foreach (string c in columns)
            {
                column.Remove(c);
            }
        }
    }

You can then use it like this:

DataTable table1 = ds.Tables[0];
table1.Columns.RemoveColumns("Column1","Column2","Column6");
BFree
the better way (o;
michl86