[英]SelectedIndexChanged Event Firing Multiple Times
i need to perform an action when the user changes the value of an DataGridViewComboBoxColumn in my grid. 当用户更改网格中DataGridViewComboBoxColumn的值时,我需要执行一个操作。 Suppose i have to show a message. 假设我必须显示一条消息。
the problem is that the messagebox.show("hello") code executes millions of times. 问题是messagebox.show(“ hello”)代码执行了数百万次。
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. 更改comboboxcell的值后,该消息仅显示一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.