此行已属于另一个表错误 [英] This row already belongs to another Table error

查看:79
本文介绍了此行已属于另一个表错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这段代码有什么问题,我想存储dt1数据dt2数据与我想存储在另一个数据中的信息不匹配数据表dt3.But在下面突出显示的行中显示错误此行已经属于另一个表

 DataTable dt3 = dt1.Clone (); 
foreach (DataRow dr1 in dt2.Rows)
{
foreach (DataRow dr2 in dt1.Rows)
{
if (dr1.ItemArray [ 0 ]。ToString()!= dr2.ItemArray [ 0 ]。ToString())
{
DataRow dr3 = dt3.NewRow();
dr3 = dr2;
dt3.Rows.Add(dr3);
}
}
}





任何人都可以帮我解决这个问题....



提前使用Thanx ....

解决方案

一行只能在一个表中。如果您想在多个表中使用相同的数据,则必须将数据复制到新的DataRow中并添加新行。



它是'就像提交一封信一样 - 原件只能在一个地方:如果你想要它在两个单独的物理文件中,你必须使用复印机。


使用importrow



 DataTable dt3 = dt1.Clone(); 
foreach(dt2.Rows中的DataRow dr1)
{
foreach(dt1.Rows中的DataRow dr2)
{
if(dr1.ItemArray [0] .ToString ()!= dr2.ItemArray [0] .ToString())
{
DataRow dr3 = dt3.NewRow();
dt3.ImportRow(dr2);
}
}
}


你必须使用 ImportRow 而不是 Rows.Add

 dt3.ImportRow(dr3); 


Hi,

What''s the wrong in this code , i want to store dt1 data dt2 data is not match that information i want to store in another datatable dt3.But in the below highlighted line it shows the error this row already belongs to another table.

DataTable dt3 = dt1.Clone();
foreach (DataRow dr1 in dt2.Rows)
{
    foreach (DataRow dr2 in dt1.Rows)
    {
        if (dr1.ItemArray[0].ToString() != dr2.ItemArray[0].ToString())
        {
            DataRow dr3 = dt3.NewRow();
            dr3 = dr2;
            dt3.Rows.Add(dr3);
        }
    }
}



can any one help me to resolve this issue....

Thanx in advance....

解决方案

A Row can only be in one table at a time. If you want the same data in more than one table, you will have to copy the data into a new DataRow and add the new row instead.

It''s just like filing a letter - the original can only be in one place: if you want it in two separate physical files, you have to use a photocopier.


use importrow

DataTable dt3 = dt1.Clone();
                    foreach (DataRow dr1 in dt2.Rows)
                    {
                        foreach (DataRow dr2 in dt1.Rows)
                        {
                            if (dr1.ItemArray[0].ToString() != dr2.ItemArray[0].ToString())
                            {
                                DataRow dr3 = dt3.NewRow();                               
                                dt3.ImportRow(dr2);                                
                            }
                        }
                    }


You have to use ImportRow instead of Rows.Add.

dt3.ImportRow(dr3);


这篇关于此行已属于另一个表错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆