该链接是正确的。处理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;
}
很高兴我能帮忙,因为我已经打开项目,所以我会随时关注您的任何问题。 – KreepN
[什么事件捕获DataGridViewCell中的组合框中的值的更改?]可能的重复(http://stackoverflow.com/questions/5652957/what-event-catches-a-change-of-value-in-a -combobox-in-a-datagridviewcell) –