0

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

newTable.Rows.Add(array);

newTable.ImportRow(row);  

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

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

循环仅“看到”使用 ImportRow 方法创建的行。好像使用 Rows.Add(array) 创建的行不存在,但显然它们确实存在,因为当我使用 DataTable 作为其数据源时,我可以在 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”语句有问题。但是什么?

4

4 回答 4

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

}

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

于 2012-08-17T02:46:18.390 回答
1

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

The ImportRow method of DataTable copies a row into a DataTable with all of the properties and data of the row. It actually calls NewRow method on destination DataTable with current table schema and sets DataRowState to Added.

于 2012-08-17T03:24:19.067 回答
0

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

但是,我无法重现您使用 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();
于 2012-08-17T01:38:53.093 回答
0

将行添加到数据表使用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;
    }
于 2012-08-17T01:56:30.613 回答