2012-04-25 51 views
0

我正在以編程方式創建一個數據網格,並且我想將已從用戶更改過的單元格的值添加到另一個數據網格中。datagrid cellvaluechanged動態對象的事件處理程序

我的代碼是這樣的:

connTo.Open(); 

SqlDataReader sqlFromReader1 = null; 
SqlDataReader sqlFromReader2 = null; 
SqlDataReader sqlFromReader3 = null; 

SqlCommand myFromCommand1 = new SqlCommand("select distinct m.catid1, c.descr "+ 
"from material m "+ 
"inner join cat1 c on c.id = m.catid1", connTo); 
sqlFromReader1 = myFromCommand1.ExecuteReader(); 

DataTable dt1 = new DataTable(); 
dt1.Load(sqlFromReader1); 

for (i = 0; i < dt1.Rows.Count; i++) 
{ 
    //int c1 = (int)(dt1.Rows[i]["catid1"]); 
    TabPage tbg = new TabPage(dt1.Rows[i]["descr"].ToString()); 
    tabControl1.Controls.Add(tbg); 

    TabControl tbi = new TabControl(); 
    tbi.Name = i.ToString() + i.ToString(); 

    SqlCommand myFromCommand2 = new SqlCommand(
     "select distinct m.catid2, c.descr " + 
     "from material m " + 
     "inner join cat2 c on c.id = m.catid2 "+ 
     "and m.catid1 = " + dt1.Rows[i]["catid1"].ToString(), connTo); 
     sqlFromReader2 = myFromCommand2.ExecuteReader(); 

    DataTable dt2 = new DataTable(); 
    dt2.Load(sqlFromReader2); 

    for (int k = 0; k < dt2.Rows.Count; k++) 
    { 
     //int c2 = (int)(dt2.Rows[k]["catid2"]); 
     TabPage tbgi = new TabPage(dt2.Rows[k]["descr"].ToString()); 
     tbi.Controls.Add(tbgi); 
     tbi.Left = 1; 
     tbi.Top = 1; 
     tbi.Width = 880; 
     tbi.Height = 400; 


     DataGridView gv = new DataGridView(); 

     gv.Width = 850; 
     gv.Height = 340;    
     gv.Left = 10; 
     gv.Top = 10; 
     gv.ColumnCount = 4; 
     gv.Columns[0].Name = "code"; 
     gv.Columns[0].Width = 120; 
     gv.Columns[0].ReadOnly = true; 

     gv.Columns[1].Name = "name"; 
     gv.Columns[1].Width = 450; 
     gv.Columns[1].ReadOnly = true; 

     gv.Columns[2].Name = "price"; 
     gv.Columns[2].Width = 80; 
     gv.Columns[2].ReadOnly = true; 

     gv.Columns[3].Name = "qty"; 
     gv.Columns[3].Width = 120; 

     SqlCommand myFromCommand3 = new SqlCommand(
      "select m.code, m.descr, m.cost1 price " + 
      "from material m " + 
      "where m.catid1 = " + dt1.Rows[i]["catid1"].ToString() + " " + 
      "and m.catid2 = " + dt2.Rows[k]["catid2"].ToString(), connTo); 

     sqlFromReader3 = myFromCommand3.ExecuteReader(); 

     DataTable dt3 = new DataTable(); 
     dt3.Load(sqlFromReader3); 

     for (int l = 0; l < dt3.Rows.Count; l++) 
     { 
      gv.Rows.Add(dt3.Rows[l]["code"].ToString(), dt3.Rows[l]["descr"].ToString(), dt3.Rows[l]["price"].ToString(), ""); 
     } 

     tbgi.Controls.Add(gv);      
    } 
    tbg.Controls.Add(tbi); 

現在我想,當用戶改變gv.Columns的價值趕上[3],請將.Name(「數量」)來添加這個值和的休息將行數據轉換爲新的數據列表。

這怎麼辦?這可能嗎?

+0

我不確定我是否理解你的問題,因爲你在問題標題中提到的事件正是我相信會幫助你的事情。我會回答,但如果這沒有幫助,請嘗試進一步解釋問題。另外,您可以使用dt3作爲datagridview的數據源,而不是像現在這樣創建行。 – 2012-04-25 22:21:40

回答

0

CellValueChanged事件應該在這種情況下工作。

當您創建的每個新DataGridView實例,並將其添加到選項卡只需添加一個事件處理程序:

dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged); 

這時你所使用的所有的事件處理程序在一個地方的方法dataGridView1_CellValueChanged

CellValueChanged事件處理程序的簽名採用DataGridViewCellEventArgs,它對於已更改的列索引和行索引都具有屬性。所以你有這樣的代碼:

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex == 3) 
    { 
     // Now you can cast sender to a DataGridView and copy the row at e.RowIndex 
     DataGridView dgv = sender as DataGridView; 
     if (dgv == null) 
      return; 

     // Here you get the cell value 
     var cellValue = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); 
    } 
} 

我也建議分配dt3作爲數據源。如果您確實使用數據源而不是未綁定的網格,則該行的複製將變得更清晰。

+0

好的,但我怎麼能有列的值,所以我可以複製它們? – 2012-04-26 05:44:10

+0

查看編輯答案 – 2012-04-26 08:41:26

相關問題