2012-06-04 46 views
0

我有一個DatagridComboboxcolumn作爲WinForms中的列之一的數據網格。 Combobox包含兩個項目Y,N。 如果用戶選擇Y,我需要更改同一行兩列的值。 當用戶選擇「N」時,會發生同樣的事情。在DatagridCombobox中更改datagrid列值selectedindexchanged事件

我試圖註冊ComboBox_SelectedIndexChanged如下。

但無法獲取所選行的行索引或庫侖索引,並更改相同行列的值。

請儘快幫助我。

private void gridTesr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
     { 
      ComboBox combo = e.Control as ComboBox;   
      if(combo != null) 
      { 
       // Remove an existing event-handler, if present, to avoid 

       // adding multiple handlers when the editing control is reused. 

       combo.SelectedIndexChanged -=new EventHandler(ComboBox_SelectedIndexChanged); 

       // Add the event handler. 

       combo.SelectedIndexChanged +=new EventHandler(ComboBox_SelectedIndexChanged); 
      } 
     } 

     private void ComboBox_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 
+0

不能使用DataGridView的* CurrentRow */* CurrentCell *屬性嗎? – Daniel

+0

是的,我可以使用currentrow/currentcell屬性。但是我在selectedchanged事件中選擇了項目後無法獲得正確的組合框值。我的活動註冊有什麼問題嗎? – Ram

回答

0

嘗試聲明自己的方法來處理ComboBox.SelectedIndexChanged事件。

private void gridTesr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
{ 
    ComboBox combo = e.Control as ComboBox;   
    if(combo != null) 
    { 
      // Remove an existing event-handler, if present 
      combo.SelectedIndexChanged -= this.MyComboEventHandler; 

      // Add the event handler 
      combo.SelectedIndexChanged += this.MyComboEventHandler; 
    } 
} 

private void MyComboEventHandler(object sender, EventArgs e) 
{ 
    string myValue = ((ComboBox)sender).SelectedItem.ToString(); 
    DataGridViewCell cell = gridTesr.CurrentCell; 

    MessageBox.Show(string.Format("The current ComboBox resides in the Row {0} and Column {1} and it currently has the {3} value.", 
      cell.RowIndex, cell.ColumnIndex, myValue)); 
} 
相關問題