C# 如何在两个现有列之间的数据集中插入一列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/351557/
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 00:10:52  来源:igfitidea点击:

How does one insert a column into a dataset between two existing columns?

c#insertdataset

提问by mezoid

I'm trying to insert a column into an existing DataSet using C#.

我正在尝试使用 C# 将一列插入到现有的 DataSet 中。

As an example I have a DataSet defined as follows:

例如,我有一个 DataSet 定义如下:

DataSet ds = new DataSet();
ds.Tables.Add(new DataTable());
ds.Tables[0].Columns.Add("column_1", typeof(string));
ds.Tables[0].Columns.Add("column_2", typeof(int));
ds.Tables[0].Columns.Add("column_4", typeof(string));

later on in my code I am wanting to insert a column between column 2 and column 4.

稍后在我的代码中,我想在第 2 列和第 4 列之间插入一列。

DataSets have methods for adding a column but I can't seem to find the best way in insert one.

数据集有添加列的方法,但我似乎无法找到插入列的最佳方法。

I'd like to write something like the following...

我想写一些类似下面的东西......

...Columns.InsertAfter("column_2", "column_3", typeof(string))

The end result should be a data set that has a table with the following columns: column_1 column_2 column_3 column_4

最终结果应该是一个数据集,其中包含一个包含以下列的表: column_1 column_2 column_3 column_4

rather than: column_1 column_2 column_4 column_3 which is what the add method gives me

而不是: column_1 column_2 column_4 column_3 这是 add 方法给我的

surely there must be a way of doing something like this.

肯定有办法做这样的事情。

Edit...Just wanting to clarify what I'm doing with the DataSet based on some of the comments below:

编辑...只是想根据以下一些评论澄清我对 DataSet 所做的事情:

I am getting a data set from a stored procedure. I am then having to add additional columns to the data set which is then converted into an Excel document. I do not have control over the data returned by the stored proc so I have to add columns after the fact.

我从存储过程中获取数据集。然后我必须向数据集添加额外的列,然后将其转换为 Excel 文档。我无法控制存储过程返回的数据,因此我必须事后添加列。

采纳答案by lomaxx

You can use the DataColumn.SetOrdinal()method for this purpose.

为此,您可以使用DataColumn.SetOrdinal()方法。

DataSet ds = new DataSet();
ds.Tables.Add(new DataTable());
ds.Tables[0].Columns.Add("column_1", typeof(string));
ds.Tables[0].Columns.Add("column_2", typeof(int));
ds.Tables[0].Columns.Add("column_4", typeof(string));
ds.Tables[0].Columns.Add("column_3", typeof(string));
//set column 3 to be before column 4
ds.Tables[0].Columns[3].SetOrdinal(2);

回答by Paul Morel

Copy the first two columns into a new dataset, then add the third column, and add the remaining columns.

将前两列复制到新数据集中,然后添加第三列,并添加其余列。

You could wrap that in an InsertAfter function if necessary.

如有必要,您可以将其包装在 InsertAfter 函数中。

回答by mezoid

I used your suggestion to create an extention method for the DataSet's DataColumnCollection:

我使用您的建议为 DataSet 的 DataColumnCollection 创建扩展方法:

public static void InsertAfter(this DataColumnCollection columns, 
                              DataColumn currentColumn, DataColumn newColumn)
{
    if (!columns.Contains(currentColumn.ColumnName))
       throw new ArgumentException(/** snip **/);

    columns.Add(newColumn);
    //add the new column after the current one
    columns[newColumn.ColumnName].SetOrdinal(currentColumn.Ordinal + 1); 
}

I can now write:

我现在可以写:

 dt = ds.Tables[0];
 dt.Columns.InsertAfter(dt.Columns["column_2"], new DataColumn("column_3"));

回答by sashoalm

Based on https://stackoverflow.com/a/17372008/492336, I use SetOrdinal with IndexOf() to insert barbefore foo:

基于https://stackoverflow.com/a/17372008/492336,我使用 SetOrdinal 和 IndexOf() 在bar之前插入foo

table.Columns.Add("bar").SetOrdinal(table.Columns.IndexOf("foo"));

To insert it after foojust add +1:

foo添加后插入+1

table.Columns.Add("bar").SetOrdinal(table.Columns.IndexOf("foo")+1);