DataTable.Add.Row(数组)和DataTable.ImportRow(行)之间的区别

时间:2012-08-17 01:16:46

标签: c# datatable

我正在尝试理解使用以下方法创建的行之间的区别:

newTable.Rows.Add(array);

newTable.ImportRow(row);  

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

的循环时
foreach (DataRow row in newTable.Rows)
{
    //...do work
}

循环仅“看到”使用ImportRow方法创建的行。它好像使用Rows.Add(数组)创建的行不存在,但显然它们可以,因为当我使用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;
    }
}

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

4 个答案:

答案 0 :(得分:1)

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

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;
}

}

你的代码只读设置所有行的列。你想设置readonly Allocation列(我的代码)。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

但是,我无法重现您使用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();

答案 3 :(得分:0)

将行添加到datatable使用 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;
    }
相关问题