2011-07-15 63 views
3

我有d的DataGridView(名爲dgvHardware)上的控制權,勢必這種控制基本WinForms應用程序是這樣的代碼:DataGridView的組合框EditingControlShowing事件

private void dgvHardware_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     if (e.Control is ComboBox) 
     { 
      ((ComboBox)e.Control).SelectionChangeCommitted -= new EventHandler(cboDgvHardware_SelectionChanged); 
      ((ComboBox)e.Control).SelectionChangeCommitted += new EventHandler(cboDgvHardware_SelectionChanged); 
     } 
    } 

    private void cboDgvHardware_SelectionChanged(object sender, EventArgs e) 
    { 
     // string result is the selected text of the combo box 
     // int row is the row index that the selected combo box lives 
     string result = ((ComboBox)sender).SelectedItem.ToString(); 
     // int row = ???? 
    } 

長話短說,我需要能夠以確定事件正在觸發的行,以便根據ComboBox選擇更改行中的其他單元格。 結果在第二個函數中返回正確的值,它是值得的。

讓我知道,如果這不清楚,或者如果你需要任何額外的信息。

感謝,
安德魯

回答

6

使用DataGridView.CurrentCell Property這樣的:

int row = dgvHardware.CurrentCell.RowIndex; 
+0

賓果!這是99%正確的,DataGridView被命名爲dgvHardware,所以完全正確的行是: 'int row = dgvHardware.CurrentCell.RowIndex;' – Fuginator

+0

@Fuginator Opps,複製並粘貼錯誤。更正! –

1

你有興趣在dgvHardware.CurrentRow,這將給你接取整行,你可以使用導航到不同的列dgvHardware.CurrentRow.Cells[index or columnName]並更改其他單元格值。