简体   繁体   中英

SelectedIndexChanged Event Firing Multiple Times

i need to perform an action when the user changes the value of an DataGridViewComboBoxColumn in my grid. Suppose i have to show a message.

the problem is that the messagebox.show("hello") code executes millions of times.

here's my code.

grilla.EditingControlShowing+=new DataGridViewEditingControlShowingEventHandler(grilla_EditingControlShowing);

void  grilla_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is ComboBox) //i have only one combobox column.
            {
                ComboBox cb = (ComboBox)e.Control;
                // first remove event handler to keep from attaching multiple:
                cb.SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged);

                // now attach the event handler
                cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
            }

        }

        void cb_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("hello");
        }

Finally y found a code that does exactly what i wanted:

grid.CurrentCellDirtyStateChanged += (s, e) => 
            {
                if (grid.IsCurrentCellDirty)
                {
                    grid.CommitEdit(DataGridViewDataErrorContexts.Commit);

                    MessageBox.Show("hello");
                }
            };

The message show only one time after change the value of the comboboxcell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM