C# 尝试添加行时,此行已属于另一个表错误?

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

This Row already belongs to another table error when trying to add rows?

c#

提问by Xaisoft

I have a DataTable which has some rows and I am using the select to filter the rows to get a collection of DataRows which I then loop through using foreach and add it to another DataTable, but it is giving me the error "This Row already belongs to another table". Here is the code:

我有一个包含一些行的数据表,我正在使用选择来过滤行以获取数据行的集合,然后我使用 foreach 循环并将其添加到另一个数据表中,但它给了我错误“该行已经属于到另一张桌子”。这是代码:

DataTable dt = (DataTable)Session["dtAllOrders"];
DataTable dtSpecificOrders = new DataTable();

DataRow[] orderRows = dt.Select("CustomerID = 2");

foreach (DataRow dr in orderRows)
{
    dtSpecificOrders.Rows.Add(dr); //Error thrown here.
}

采纳答案by JoshBerke

You need to create a new Rowwith the values from drfirst. A DataRowcan only belong to a single DataTable.

您需要Row使用dr第一次的值创建一个新的。ADataRow只能属于一个DataTable.

You can also use Addwhich takes an array of values:

您还可以使用Addwhich 接受一组值:

myTable.Rows.Add(dr.ItemArray)

Or probably even better:

或者甚至更好:

// This works because the row was added to the original table.
myTable.ImportRow(dr);

// The following won't work. No data will be added or exception thrown.
var drFail = dt.NewRow()
drFail["CustomerID"] = "[Your data here]";
// dt.Rows.Add(row); // Uncomment for import to succeed.
myTable.ImportRow(drFail);

回答by Shekhar Purwar

Try this:

尝试这个:

DataTable dt = (DataTable)Session["dtAllOrders"];
DataTable dtSpecificOrders = dt.Clone();

DataRow[] orderRows = dt.Select("CustomerID = 2");

foreach (DataRow dr in orderRows)
{
    dtSpecificOrders.ImportRow(dr);
}

回答by jersoft

yourTable.ImportRow(dataRow);

It's because the row you're copying doesn't have the same TableName:

这是因为您正在复制的行没有相同的TableName

For example, try:

例如,尝试:

Table1.TableName = "Table1";
Table2.TableName = "Table2";

回答by Avinash Singh

foreach (DataRow dr in dtSpecificOrders.rows)
{
   dtSpecificOrders.Rows.Add(dr.ItemArray); 
}

回答by MSTr

Why don't you just use CopyToDataTable

你为什么不直接使用 CopyToDataTable

DataTable dt = (DataTable)Session["dtAllOrders"];
DataTable dtSpecificOrders = new DataTable();

DataTable orderRows = dt.Select("CustomerID = 2").CopyToDataTable();

回答by vapcguy

This isn't the cleanest/quickest/easiest/most elegant solution, but it is a brute force one that I created to get the job done in a similar scenario:

这不是最干净/最快/最简单/最优雅的解决方案,但它是我为在类似场景中完成工作而创建的一种蛮力解决方案:

DataTable dt = (DataTable)Session["dtAllOrders"];
DataTable dtSpecificOrders = new DataTable();

// Create new DataColumns for dtSpecificOrders that are the same as in "dt"
DataColumn dcID = new DataColumn("ID", typeof(int));
DataColumn dcName = new DataColumn("Name", typeof(string));
dtSpecificOrders.Columns.Add(dtID);
dtSpecificOrders.Columns.Add(dcName);

DataRow[] orderRows = dt.Select("CustomerID = 2");

foreach (DataRow dr in orderRows)
{
    DataRow myRow = dtSpecificOrders.NewRow();  // <-- create a brand-new row
    myRow[dcID] = int.Parse(dr["ID"]);
    myRow[dcName] = dr["Name"].ToString();
    dtSpecificOrders.Rows.Add(myRow);   // <-- this will add the new row
}

The names in the DataColumns must match those in your original table for it to work. I just used "ID" and "Name" as examples.

DataColumns 中的名称必须与原始表中的名称匹配才能工作。我只是以“ID”和“Name”为例。

回答by Adi_Pithwa

you can give some id to the columns and name it uniquely.

您可以为列指定一些 id 并对其进行唯一命名。