How can I delete a column of a Dataset?
Asked Answered
G

6

6

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])
Giliane answered 23/3, 2009 at 14:39 Comment(0)
G
21

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");
Gelatinous answered 23/3, 2009 at 14:42 Comment(0)
Z
4

I have done this before in both winforms & web apps. You must go thru the DataTable in reverse and then do an AcceptChanges at the end. This particular example is typical, in that it removes all the GUIDs (columns ending with "id").

    private void RemoveUselessFields(ref DataTable dtResults)
    {
        for (int i = dtResults.Columns.Count - 1; i >= 0; i--)
        {
            DataColumn column = dtResults.Columns[i];
            if (column.ColumnName.Substring(column.ColumnName.Length - 2, 2).ToUpper() == "ID") 
            {
                dtResults.Columns.Remove(column);
            }
        }
        dtResults.AcceptChanges();

    }
Zealot answered 17/10, 2011 at 21:22 Comment(0)
E
3

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");
Eurypterid answered 23/3, 2009 at 14:47 Comment(0)
G
2

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]);
Georgy answered 23/3, 2009 at 14:45 Comment(0)
I
0

if ever you want to delete the columns because you have new set of columns names, you can use this:

DataSet.Reset();

Hope it helps.

Integrated answered 4/9, 2013 at 9:27 Comment(0)
L
0

In python, there is a package called pandas. It has a function called drop. pandas.dataframe.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise') Here, use the name of the column to be dropped at labels="column name".

refer more in pandas documentation :https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.drop.html

Liggitt answered 18/11, 2021 at 2:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.