【问题标题】:Difference Between DataTable.Add.Row(array) and DataTable.ImportRow(row)DataTable.Add.Row(array) 和 DataTable.ImportRow(row) 之间的区别
【发布时间】:2012-08-17 01:16:45
【问题描述】:

我试图了解使用以下方法创建的行之间的区别:

newTable.Rows.Add(array);

newTable.ImportRow(row);  

我有一个 WinForms 应用程序,它使用这两种方法向 DataTable 添加行。 现在,当我将该 DataTable 用作 DataGridView 的数据源时,我可以看到使用这两种方法创建的“所有”行。 但是,当我使用类型的循环时

foreach (DataRow row in newTable.Rows)
{
    //...do work
}

循环仅“看到”使用 ImportRow 方法创建的行。好像使用 Rows.Add(array) 创建的行不存在,但显然它们确实存在,因为当我使用 DataTable 作为其 DataSource 时,我可以在 DataGridView 中看到它们。

编辑(随后由 Rahil Jan Muhammad 发表评论)

是的 - 在玩了很多之后,我认为行添加方法与它无关。问题出在以下代码中:

foreach (DataGridViewColumn col in dataAllocations.Columns)
{
    if (col.Name == "Allocation")
    {
        col.ReadOnly = false;
    }
    else
    {
        col.ReadOnly = true;
    }
}  
foreach (DataGridViewRow row in dataAllocations.Rows)
{
    MessageBox.Show("DataGrid, " + row.Cells["AllocID"].Value.ToString() +
                                ", " + row.Cells["Active"].Value.ToString());

    if (row.Cells["Active"].Value == null || (bool)row.Cells["Active"].Value == false)
    {                        
         row.ReadOnly = true;
    }
    else
    {
         row.ReadOnly = false;
    }
}

第一个循环使除“分配”列之外的所有列都是只读的。如果“活动”列中的值为假,则第二个循环旨在使“分配”列甚至为只读。然而,正在发生的事情恰恰相反。 Active 为 true 的那些行是只读的,反之亦然。所以是的,我的“if”语句有问题。但是什么?

【问题讨论】:

  • 请参考下面的链接了解差异:stackoverflow.com/questions/5140158/… 就您的问题而言,您无法访问使用 newTable.Rows.Add(array) 添加的行。请提供您的代码,以便我们查看。
  • 我现在添加了更多代码 - 谢谢。
  • 在你的消息框中尝试添加 Cells["Active"] 的值。我猜这个 IF 条件有问题

标签: c# datatable


【解决方案1】:

我发现了你的问题(日志错误):

foreach (DataGridViewRow row in dataAllocations.Rows)
{
 MessageBox.Show("DataGrid, " + row.Cells["AllocID"].Value.ToString() +
                            ", " + row.Cells["Active"].Value.ToString());

if (row.Cells["Active"].Value == null || (bool)row.Cells["Active"].Value == false)
{                        
     row.Cells["Allocation"].ReadOnly = true;
}
else
{
     row.Cells["Allocation"].ReadOnly = false;
}

}

您的代码将所有行的列设置为只读。您想设置只读分配列(我的代码)。

【讨论】:

    【解决方案2】:

    datatable.Rows.Add() 要将新记录添加到数据集中,必须创建一个新数据行并将其添加到数据集中 DataTable 的 DataRow 集合 (Rows)

    DataTable 的 ImportRow 方法将一行复制到 DataTable 中,其中包含该行的所有属性和数据。它实际上使用当前表架构在目标 DataTable 上调用 NewRow 方法,并将 DataRowState 设置为已添加。

    【讨论】:

      【解决方案3】:

      Rows.Add 和 ImportRow 之间的区别在于,如果您调用 Rows.Add,则该行被标记为已添加(因此,如果您将表发送到数据适配器,则会生成插入)。如果使用Import row,则行进来了,但不是“inserted”状态,不会生成insert。

      但是,我无法重现您使用 AddRow 描述的行为 - 它们出现在我的 foreach 中。

      下面的测试代码(这是来自 LINQpad,如果没有,可以将 .Dump() 调用更改为 Console.Write):

      var dt = new DataTable(); dt.Columns.Add("C1", typeof(int)); for(int i = 0; i < 10; i++) { var row = dt.NewRow(); row[0] = i; dt.Rows.Add(row); } foreach(DataRow row in dt.Rows) row.Dump();

      【讨论】:

        【解决方案4】:

        将行添加到数据表使用 newTable.Rows.Add(array); 并将行从其他表添加到另一个表,我们使用 newTable.ImportRow(row);

        示例:

                DataTable dt = GetTable(true);
        
                dataGridView1.DataSource = dt;
        
        
                DataRow row = dt.NewRow();
                row[0] = 101;
                row[1] = "101";
                row[2] = "101";
                row[3] = DateTime.Now;
        
        
                dt.ImportRow(row);
        
                DataTable dt2 = GetTable(false);
        
                object[] r = { 105, "Habib", "Zare", DateTime.Now };
                dt2.Rows.Add(r);
        
                dt2.ImportRow(dt.Rows[1]);
                dataGridView2.DataSource = dt2;
        

        GetTable 方法:

            static DataTable GetTable(bool isAddRows)
            {
        
                DataTable table = new DataTable();
                table.Columns.Add("Dosage", typeof(int));
                table.Columns.Add("Drug", typeof(string));
                table.Columns.Add("Patient", typeof(string));
                table.Columns.Add("Date", typeof(DateTime));
        
        
                if (isAddRows)
                {
                    table.Rows.Add(25, "Indocin", "David", DateTime.Now);
                    table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
                    table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
                    table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
                    table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
                }
        
                return table;
            }
        

        【讨论】:

          猜你喜欢
          • 2018-08-30
          • 2012-05-21
          • 2019-01-28
          • 2016-05-12
          • 2012-12-23
          • 2021-06-10
          • 2016-05-03
          • 1970-01-01
          相关资源
          最近更新 更多