Problem with DataGridView ComboBox event handler

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) { // Do some amazing stuff... } 

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) { // the event to handle combo changes EventHandler comboDelegate = new EventHandler( (cbSender, args) => { DoSomeStuff(); }); // register the event with the editing control combo.SelectedValueChanged += comboDelegate; // since we don't want to add this event multiple times, when the // editing control is hidden, we must remove the handler we added. EventHandler visibilityDelegate = null; visibilityDelegate = new EventHandler( (visSender, args) => { // remove the handlers when the editing control is // no longer visible. if ((visSender as Control).Visible == false) { combo.SelectedValueChanged -= comboDelegate; visSender.VisibleChanged -= visibilityDelegate; } }); (sender as DataGridView).EditingControl.VisibleChanged += visibilityDelegate; } }" 

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.

+4
source share
1 answer

It looks like you want the changes to be made as soon as the user has changed the drop-down list, without having to click on this cell. To do this, you will need to force a commit when the change occurs (using CommitEdit , there is also an example on the MSDN page). Add this to your DataGridView :

 // This event handler manually raises the CellValueChanged event // by calling the CommitEdit method. void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) { if (dataGridView1.IsCurrentCellDirty) { dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); } } 

Then you can just listen to CellValueChanged and not try to register for the ComboBoxValueChanged event in the basic edit control.

+13
source

Source: https://habr.com/ru/post/1368989/


All Articles