2

当用户更改我的网格中 DataGridViewComboBoxColumn 的值时,我需要执行一个操作。假设我必须显示一条消息。

问题是 messagebox.show("hello") 代码执行了数百万次。

这是我的代码。

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");
        }
4

1 回答 1

1

最后,你找到了一个完全符合我要求的代码:

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

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

更改组合框单元格的值后,该消息仅显示一次。

于 2013-02-01T14:16:50.093 回答