2012-08-17 40 views
0

我想了解使用下面的方法創建的行之間的差異之間:差異DataTable.Add.Row(陣列)和DataTable.ImportRow(行)

newTable.Rows.Add(array); 

newTable.ImportRow(row); 

我有一個WinForms應用程序,它使用這兩種方法將行添加到DataTable。 現在,當我使用該DataTable作爲DataGridView的DataSource時,我可以看到使用這兩種方法創建的「全部」行。 然而,當我使用類型

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

的循環迴路只「看到」使用ImportRow方法創建的行。它好像使用Rows.Add(array)創建的行不存在,但顯然它們的確如此,因爲當我使用DataTable作爲它的DataSource時,我可以在DataGridView中看到它們。

EDIT(隨後由拉希爾一月穆罕默德評論)

是 - 大量的玩耍後,我覺得行添加的方法是什麼,用它做。問題出現在以下代碼中:

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

第一個循環使除列「Allocation」之外的所有列爲只讀。如果列「Active」中的值爲false,則第二個循環用於使列「Allocation」爲只讀。然而,發生的事情恰恰相反。 Active爲true的行是隻讀的,反之亦然。所以是的,我的'if'聲明有問題。但是什麼?

+0

請參閱下面的鏈接區別: http://stackoverflow.com/questions/5140158/difference-between-rows-add-and-importrow只要您的問題擔心,您不能訪問添加的行使用newTable.Rows.Add(數組)。請提供您的代碼,以便我們看看。 – 2012-08-17 01:33:11

+0

我已經添加了更多代碼 - 謝謝。 – PJW 2012-08-17 01:37:50

+1

在你的messagebox中嘗試添加Cells [「Active」]的值。我想這個IF條件有問題 – 2012-08-17 02:02:50

回答

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

}

自己的代碼集所有行的列只讀。您想設置只讀分配列(我的代碼)。

+0

row.Cells [「Allocation」]。ReadOnly = false;作品 - 謝謝 – PJW 2012-08-17 03:26:04

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(); 
0

到行添加到數據表中使用newTable.Rows.Add(數組);並將其他表中的行添加到我們使用的另一個表中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; 
    } 
1

datatable.Rows。添加() 添加新記錄到數據集,新的數據行,必須創建並添加到DataRow中收集數據表(行)的數據集中

數據表拷貝一行到的ImportRow方法包含該行所有屬性和數據的DataTable。它實際上使用當前表模式在目標DataTable上調用NewRow方法,並將DataRowState設置爲Added。