I am having a problem handling an indexed event for a comboBox that is inside a dataGridView. I am writing a method to handle comboBox selection change using a delegate:
ComboBox.SelectedIndexChanged -= delegate { ComboBoxIndexChanged(); }; ComboBox.SelectedIndexChanged += delegate { ComboBoxIndexChanged(); };
or EventHandler:
comboBox.SelectedIndexChanged += new EventHandler(ComboBoxIndexChanged);
but both methods do not work as expected. That is, when you click on your choice in a comboBox (contained in a gridview data file), it takes a few clicks to call my ComboBoxIndexChanged (); a method for proper functioning if it decides to function at all. What is the best way to overcome / transition to event definition in comboBox indexed element inside dataGridView?
The code that I am currently using in context is as follows:
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { try { if (this.dataGridView.CurrentCell.ColumnIndex == (int)Column.Col) { ComboBox comboBox = e.Control as ComboBox; if (comboBox != null) { comboBox.SelectedIndexChanged += new EventHandler(ComboBoxIndexChanged); } } return; } catch (Exception Ex) { Utils.ErrMsg(Ex.Message); return; } }
and the ComboBoxIndexChanged event:
private void ComboBoxIndexChanged(object sender, EventArgs e) {
I read a similar thread in StackOverFlow that says there is a problem handling the comboBox change event this way, but I can't get this solution to work. The message can be found here: "SelectedIndexChanged" events in the ComboBoxColumn in the Datagridview . It says:
โThings get complicated because they optimized the DataGridView with only one edit control for all rows. Here, as I dealt with a similar situation:
First connect the delegate to the EditControlShowing event:
myGrid.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler( Grid_EditingControlShowing); ...
Then in the handler, connect to the EditControl SelectedValueChanged event:
void Grid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox combo = e.Control as ComboBox; if (combo != null) {
This problem is due to the fact that "VisSender" is not defined, so the "VisibleChanged" event cannot be used.
Any help from you guys, as always, is most appreciated.