当用户更改网格中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");
}
最佳答案
最后,您找到了完全符合我想要的代码:
grid.CurrentCellDirtyStateChanged += (s, e) =>
{
if (grid.IsCurrentCellDirty)
{
grid.CommitEdit(DataGridViewDataErrorContexts.Commit);
MessageBox.Show("hello");
}
};
更改comboboxcell的值后,该消息仅显示一次。
关于c# - SelectedIndexChanged事件多次触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14608491/