2012-06-21 445 views
15

我有DataGridView有兩列。第一列是TextBoxCol(DataGridViewTextBoxColumn),第二列是ComboBoxCol(DataGridViewComboBoxColumn)在DataGridViewComboBoxColumn中觸發的事件SelectedIndexChanged

ComboBoxCol更改其選定索引值時,如何更改TextBoxCol的值? (當選擇指數ComboBoxCol改變這應該發生的。離開塔後不一樣,dataGridView_CellValueChanged

我看過一個話題幾乎,我有同樣的問題,但我不明白的答案(這應該是正確的基於複選標記)。這是鏈接。 -Almost same topic

+0

很高興我能幫忙,因爲我已經打開項目,所以我會隨時關注您的任何問題。 – KreepN

+1

[什麼事件捕獲DataGridViewCell中的組合框中的值的更改?]可能的重複(http://stackoverflow.com/questions/5652957/what-event-catches-a-change-of-value-in-a -combobox-in-a-datagridviewcell) –

回答

23

給這兩個簡單的方法去(即「1 '在最上面的方法是組合框的索引)

你會讓你修改的行將是setter行cel.Value =,因爲你可以改變它爲任何你喜歡的。


private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     if (dataGridView1.CurrentCell.ColumnIndex == 1 && e.Control is ComboBox) 
     { 
      ComboBox comboBox = e.Control as ComboBox; 
      comboBox.SelectedIndexChanged -= LastColumnComboSelectionChanged; 
      comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged; 
     } 
    } 

    private void LastColumnComboSelectionChanged(object sender, EventArgs e) 
    { 
     var currentcell = dataGridView1.CurrentCellAddress; 
     var sendingCB = sender as DataGridViewComboBoxEditingControl; 
     DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0]; 
     cel.Value = sendingCB.EditingControlFormattedValue.ToString(); 
    } 

enter image description here

+8

確保你還用'comboBox.SelectedIndexChanged - = LastColumnComboSelectionChanged;' – Smashery

2

該鏈接是正確的。處理DataGridView的EditingControlShowing event。在這個事件處理程序中,檢查當前列是否符合您的興趣。和,然後創建一個臨時組合框對象: -

ComboBox comboBox = e.Control as ComboBox;

MSDN具有樣品:參見實施例部分here注意在MSDN鏈接Inheritance Hierarchy & Class Syntax: -

公共類DataGridViewComboBoxEditingControl:組合框, IDataGridViewEditingControl

private DataGridView dataGridView1 = new DataGridView(); 

private void AddColorColumn() 
{ 
    DataGridViewComboBoxColumn comboBoxColumn = 
     new DataGridViewComboBoxColumn(); 
    comboBoxColumn.Items.AddRange(
     Color.Red, Color.Yellow, Color.Green, Color.Blue); 
    comboBoxColumn.ValueType = typeof(Color); 
    dataGridView1.Columns.Add(comboBoxColumn); 
    dataGridView1.EditingControlShowing += 
     new DataGridViewEditingControlShowingEventHandler(
     dataGridView1_EditingControlShowing); 
} 

private void dataGridView1_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) 
{ 
    ((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem; 
} 
28

這個答案是在幾個地方左右浮動。使用DataGridViewEditingControlShowingEventHandler將觸發比您想要的更多的事件。在我的測試中,它多次啓動了該事件。同樣使用combo.SelectedIndexChanged - =事件不會真的刪除事件,它們只是保持堆疊。無論如何,我找到了一個似乎可行的解決方案。我包括下面的代碼示例:

  // Add the events to listen for 
     dataGridView1.CellValueChanged += 
      new DataGridViewCellEventHandler(dataGridView1_CellValueChanged); 
     dataGridView1.CurrentCellDirtyStateChanged += 
      new EventHandler(dataGridView1_CurrentCellDirtyStateChanged); 

    // This event handler manually raises the CellValueChanged event 
    // by calling the CommitEdit method. 
    void dataGridView1_CurrentCellDirtyStateChanged(object sender, 
     EventArgs e) 
    { 
     if (dataGridView1.IsCurrentCellDirty) 
     { 
      // This fires the cell value changed handler below 
      dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); 
     } 
    } 

    private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
    { 
     // My combobox column is the second one so I hard coded a 1, flavor to taste 
     DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells[1]; 
     if (cb.Value != null) 
     { 
       // do stuff 
       dataGridView1.Invalidate(); 
     } 
    } 
+1

刪除任何現有的事件處理程序。這確實比接受的解決方案更好。只有我必須改變的是添加如果(例如。RowIndex == -1) return;到dataGridView1_CellValueChanged函數的開頭 – Kevin

+0

偉大的解決方案,那應該被接受 –

+2

是的,絕對是一個更好的解決方案,選定的人多次觸發事件。 –

相關問題