简体   繁体   English

向DataTable问题添加行

[英]Adding a Row to a DataTable question

Greetings everyone- 大家问候

In my code below I'm trying to add a Row from an existing DataTable (dtResult) into a new DataTable (dtCopyResult) if email address does not match. 在下面的代码中,如果电子邮件地址不匹配,我尝试将现有DataTable(dtResult)中的行添加到新DataTable(dtCopyResult)中。 So I guess my knowledge of ADO.NET is not up to par because whenever I try to run my below code, I get an "This Row already belongs to another table". 因此,我想我对ADO.NET的了解还不够,因为每当我尝试运行下面的代码时,我都会得到“此行已属于另一个表”。 Please let me know how I can fix this.. 请让我知道如何解决此问题。

Many Thanks 非常感谢

 if (checkBox1.Checked)  
                    {

                        for (int i = dtResult.Rows.Count - 1; i >= 0; i--)  //dtResult is a DataTable
                        {
                            foreach (object email in emails)  //emails is an ArrayList of email addresses
                            {
                                if (email.ToString().ToUpper() != dtResult.Rows[i][3].ToString().ToUpper())
                                {
                                    dtCopyResult.Rows.Add(dtResult.Rows[i]);  //dtCopyResult is a new blank DataTable that I'm trying to add rows to
                                }
                            }

                        }

                    }

As the error message tells you, a DataRow belongs to a particular DataTable; 如错误消息所示,DataRow属于特定的DataTable。 you cannot just take it and add it to another one. 您不能只接受它并将其添加到另一个。 What you can do is either 您可以做的就是

  • create a new DataRow and fill it with the values from the old DataRow or 创建一个新的 DataRow并用旧DataRow中的值填充它,或

  • use the DataTable.ImportRow method: 使用DataTable.ImportRow方法:

     dtCopyResult.ImportRow(dtResult.Rows[i]); 

您可以使用ImportRow函数,完整的示例在这里http://support.microsoft.com/kb/308909

One thing I noticed is that the new row will get added multiple times; 我注意到的一件事是,新行将被添加多次。 once for each item in the emails collection. 电子邮件集合中的每个项目一次。 You either need to keep a local list of items already added or loop through dtCopyResult to make sure you have not already added the email. 您需要保留一个已经添加的项目的本地列表,或者遍历dtCopyResult以确保您尚未添加电子邮件。

List<string> alreadyAdded = new List<string>();

if (email.ToString().ToUpper() != dtResult.Rows[i][0].ToString().ToUpper() 
    && !alreadyAdded.Contains(email.ToString()))
{
    dtCopyResult.ImportRow(_dt1.Rows[i]);
    alreadyAdded.Add(email.ToString());
}

it means the adding row is belong to "dtResult" and DataRow is an "Object" that represent data. 这意味着添加的行属于“ dtResult”,而DataRow是表示数据的“对象”。 Not data itselfs. 不是数据本身。 so in this case u try to add DataRow object that belong to another table which will error. 所以在这种情况下,您尝试添加属于另一个表的DataRow对象,这将出错。

another way to do is copy everything to dtCopy and delete it if condition mismatch. 另一种方法是将所有内容复制到dtCopy并在条件不匹配时将其删除。 Array is mainly to used to stored various type of object. 数组主要用于存储各种类型的对象。 In this case, if u gonna store only email u should use 在这种情况下,如果您只存储电子邮件,则应使用

List<string> emails = new List<string>();
emails.Add("test@example.com");

to enumerate rows for deleteing data 枚举删除数据的行

List<DataRow> removing = new List<DataRow>();
foreach(var row in table.AsEnumerable()) // or table.Rows
if(...condition)removing.Add(row);

foreach(var row in removing)table.Rows.Remove(row);

the reason to use 'removing' is if u loop through rows and removing it at the same time, means u change the enumerate which will cause error. 使用“删除”的原因是,如果您在行中循环并同时删除它,则意味着您更改了枚举,这将导致错误。 becus .Net is not happy when u pull out something its looping. 当您拉出其循环时,becus .Net不高兴。

Try replacing 尝试更换

 dtCopyResult.Rows.Add(dtResult.Rows[i]);

with

DataRow rowToBeCopied = dtCopyResult.NewRow();
//Copy the row values..
rowToBeCopied.X = dtResult.Rows[i].X;
//Copy the remaining row values
dtCopyResult.Rows.Add(rowToBeCopied);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM