C# 合并 2 个数据表并存储在一个新的数据表中

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

Merge 2 DataTables and store in a new one

c#datatable

提问by Xaisoft

If I have 2 DataTables (dtOne and dtTwo) and I want to merge them and put them in another DataTable (dtAll). How can I do this in C#? I tried the Merge statement on the datatable, but this returns void. Does Merge preserve the data? For example, if I do:

如果我有 2 个数据表(dtOne 和 dtTwo)并且我想合并它们并将它们放在另一个数据表(dtAll)中。我怎样才能在 C# 中做到这一点?我在数据表上尝试了 Merge 语句,但这返回无效。合并是否保留数据?例如,如果我这样做:

 dtOne.Merge(dtTwo);

Does dtOne change or does dtTwo change and if either one changes, do the changes preserve?

dtOne 是否更改或 dtTwo 是否更改,如果其中任何一个更改,更改是否保留?

I know I can't do this because Merge returns void, but I want to be able to store the Merger of both dtOne and dtTwo in dtAll:

我知道我不能这样做,因为 Merge 返回 void,但我希望能够将 dtOne 和 dtTwo 的合并存储在 dtAll 中:

//Will Not work, How do I do this
dtAll = dtOne.Merge(dtTwo);

采纳答案by Jeromy Irvine

The Mergemethod takes the values from the second table and merges them in with the first table, so the first will now hold the values from both.

Merge方法从第二个表中获取值并将它们与第一个表合并,因此第一个现在将保存两个表中的值。

If you want to preserve both of the original tables, you could copy the original first, then merge:

如果要保留两个原始表,可以先复制原始表,然后合并:

dtAll = dtOne.Copy();
dtAll.Merge(dtTwo);

回答by Jeromy Irvine

dtAll = dtOne.Copy();
dtAll.Merge(dtTwo,true);

The parameter TRUE preserve the changes.

参数 TRUE 保留更改。

For more details refer to MSDN.

有关更多详细信息,请参阅MSDN

回答by rami220

DataTable dtAll = new DataTable();
DataTable dt= new DataTable();
foreach (int id in lst)
{
    dt.Merge(GetDataTableByID(id)); // Get Data Methode return DataTable
}
dtAll = dt;

回答by SNag

(Quite late, but might help someone stumbling upon this question.)

Instead of dtAll = dtOne.Copy();in Jeromy Irvine's answeryou can do:

(很晚了,但可能会帮助那些在这个问题上绊倒的人。)

而不是dtAll = dtOne.Copy();杰罗米欧文的回答中,你可以这样做:

dtAll = new DataTable();
...
dtAll.Merge(dtOne);
dtAll.Merge(dtTwo);
dtAll.Merge(dtThree);
...

and so on.

等等。

This technique is useful in a loop where you want to iteratively merge data tables:

此技术在您想要迭代合并数据表的循环中很有用:

DataTable dtAllCountries = new DataTable();

foreach(String strCountry in listCountries)
{
    DataTable dtCountry = getData(strCountry); //Some function that returns a data table
    dtAllCountries.Merge(dtCountry);
}

回答by anand360

This is what i did for merging two datatables and bind the final result to the gridview

这就是我为合并两个数据表并将最终结果绑定到 gridview 所做的

        DataTable dtTemp=new DataTable();
        for (int k = 0; k < GridView2.Rows.Count; k++)
        {
            string roomno = GridView2.Rows[k].Cells[1].Text;
            DataTable dtx = GetRoomDetails(chk, roomno, out msg);
            if (dtx.Rows.Count > 0)
            {
                dtTemp.Merge(dtx);
                dtTemp.AcceptChanges();

            }
        }