I have a DataGridViewComboBoxColumn in a data grid view. I have attached a list as a data source . Now i need to fire an event based on the selected index of the combobox..How c
Given that the SelectedIndex
property belongs to the editing control (which is only active when the DataGridView
is in edit mode), you could attach an event handler to EditingControlShowing
like so:
void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
if (e.Control is ComboBox) {
// remove handler first to avoid attaching twice
((ComboBox)e.Control).SelectedIndexChanged -= MyEventHandler;
((ComboBox)e.Control).SelectedIndexChanged += MyEventHandler;
}
}
Note that the actual type of the control is DataGridViewComboBoxEditingControl
, which extends ComboBox
. You only need the functionality from the base class, plus it's less to type.