为什么当传递新创建的DataRow DataTable.Rows.ImportRow不起作用? [英] Why DataTable.Rows.ImportRow doesn't work when passing new created DataRow?

查看:827
本文介绍了为什么当传递新创建的DataRow DataTable.Rows.ImportRow不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加 NEWROW MyHandler的,它应该是行添加到从内部构建表该办法传递给了MyHandler的。
现在的问题是,当我使用 dt.ImportRow(行); 它不添加任何行。
如果我添加 NEWROW T 表,然后做 handler.Add( t.row [t.Rows.Count - 1]); 所以它的工作原理,但我不希望添加到 T 表。在 T 只是让我可以创建新行。



为什么 dt.ImportRow(行); 不起作用? ?如何解决这个问题。



  {
变种T =新的DataTable();
t.Columns.Add(新的DataColumn(COL1,typeof运算(字符串)));
t.Columns.Add(新的DataColumn(col2的的typeof(字节)));
t.Columns.Add(新的DataColumn(COL3的typeof(字节)));
t.Rows.Add(一,1,1);
t.Rows.Add(B,2,2);

//建立MyHandler的从计划
变种处理器内部表= MyHandler的新(t.Clone());

VAR NEWROW = t.NewRow();
行[COL1] =C;
行[col2的] = 3;
行[COL3] = 3;

handler.Add(行);
}

类公共MyHandler的
{
的DataTable dt的;
类公共MyHandler的(数据表方案)
{
DT =计划;
}
公共无效添加(DataRow的行)
{
dt.ImportRow(行);
}
}


解决方案

您应该只使用 ImportRow 从另一个DataTable中添加行。每 MSDN ,行与<$ C创建应 DataTable.Rows.Add 添加$ C> DataTable.NewRow 。


I need to add newRow to MyHandler and it should add that row to the table that was built internally from the "scheme" that was passed to the MyHandler. The problem is when I use dt.ImportRow(row); it doesn't add any row. If I add newRow to t table and then do handler.Add(t.row[t.Rows.Count - 1]); so it works, but I don't want to add to t table. The t is just so I could create new row.

Why dt.ImportRow(row); doesn't work? How to fix it?

{
   var t = new DataTable();
   t.Columns.Add(new DataColumn("Col1", typeof(string)));
   t.Columns.Add(new DataColumn("Col2", typeof(byte)));
   t.Columns.Add(new DataColumn("Col3", typeof(byte)));
   t.Rows.Add("a", 1, 1);
   t.Rows.Add("b", 2, 2);

   // MyHandler build internal table from the scheme
   var handler = new MyHandler(t.Clone());

   var newRow = t.NewRow();
   row["Col1"] = "c";
   row["Col2"] = 3;
   row["Col3"] = 3;

   handler.Add(row);
}

public class MyHandler
{
    DataTable dt;
    public class MyHandler(DataTable scheme)
    {
        dt = scheme;
    }
    public void Add(DataRow row)
    {
       dt.ImportRow(row);
    }
}

解决方案

You should only use ImportRow to add rows from another DataTable. Per MSDN, rows created with DataTable.NewRow should be added with DataTable.Rows.Add.

这篇关于为什么当传递新创建的DataRow DataTable.Rows.ImportRow不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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