C# 如何删除数据集的列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/673616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:51:59  来源:igfitidea点击:

How can I delete a column of a Dataset?

c#.netdataset

提问by Penguen

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])

采纳答案by Marc Gravell

First, a DataTablehas columns, not a data-set.

首先, aDataTable有列,而不是数据集。

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).

如果您有列索引,则应该完成这项工作。请注意,如果您删除第 0 列,则数字将随机排列(因此您可能需要以相反的顺序进行)。

Alternatively, you may want to remove by name:

或者,您可能希望按名称删除:

table.Columns.Remove("Foo");

回答by Michael Piendl

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

没有这样的方法。但是您可以简单地编写自己的 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]);

回答by BFree

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");

回答by user1000008

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").

我之前在 winforms 和 web 应用程序中都做过这个。您必须反向遍历 DataTable,然后在最后执行 AcceptChanges。这个特定示例是典型的,因为它删除了所有 GUID(以“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();

    }

回答by SyntaxError

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.

希望能帮助到你。