2012-06-21 392 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

是的,绝对是一个更好的解决方案,选定的人多次触发事件。 –

相关问题