2016-10-25 54 views
0

我使用下面的代碼填充我的datagridview。在datagridview中更新組合框列

dgvPChannel.AutoGenerateColumns = true; 
     dgvPChannel.DataSource = new PaymentsAccess().getAllComplianceAccounts().ToList(); 

我在datagridview中創建了一個額外的列,並填充了這個組合框列。將此組合框更改爲「更新」按鈕時,現在需要更新數據庫。如何使用爲每個項目選擇的組合框選項更新所有的datagridview項目。

回答

1

如果循環遍歷網格中的每個ComboBox值,則可以更新cheched的行。檢查:

private void btnUpdate_Click(object sender, EventArgs e) 
    { 

     foreach (DataGridViewRow row in yourdataGridView.Rows) 
     { 
      var comboValue = string.IsNullOrEmpty(row.Cells[ComboBoxColumnName.Index].Value.ToString()) ? "" : row.Cells[ComboBoxColumnName.Index].Value.ToString(); 
      if (some logic here to update) 
      { 
       //update your_table set field = value where id = row.Cells["fieldname"].Value; 


      } 
     } 

    } 
+0

其組合框不復選框 – user3430861

+0

好的非常感謝你,我會努力在這一點,讓你知道,不管怎樣我怎麼設置我的組合框中選定的項目。例如,如果row.cells [1] .value ==「A」combox selectedindex 1.請幫忙嗎? – user3430861

+0

從來沒有得到我的答案 - http://stackoverflow.com/questions/4825222/how-to-set-selectedindex-in-datagridviewcomboboxcolumn – user3430861