該鏈接是正確的。處理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) –