各位大侠,我在datagridview中创建了一个combobox列,然后要写SelectedIndexChanged函数如下所示:
private void dataGridView_CurveDivide_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType().Equals(typeof(DataGridViewComboBoxEditingControl)))
{
e.CellStyle.BackColor = System.Drawing.Color.FromName("window");
DataGridViewComboBoxEditingControl editingControl = e.Control as DataGridViewComboBoxEditingControl;
editingControl.SelectedIndexChanged+=new EventHandler(editingControl_SelectedIndexChanged);
}
}
public void editingControl_SelectedIndexChanged(object sender, EventArgs e)
{
string WallType = dataGridView_CurveDivide.CurrentCell.EditedFormattedValue.ToString();
if (WallType.Equals("墙"))
{
Form wall=new Form();
wall.ShowDiag();
}
}
在运行的时候我点击combobox的选项“墙”是可以运行窗体的,但是我的combobox列有3行,我点击第二行的combobox列选择“墙”时,窗体跳出来两次,点击第3行时窗体跳出来3次,再点击第1行时窗体跳出来4次,我发现他每次都记录点击次数的,然后叠加,然后窗体就根据总共点击的次数决定跳出来多少次。。。。。
各位大侠这是为什么啊???
------解决思路----------------------
要么你就在
editingControl_SelectedIndexChanged回调函数里
执行
editingControl.SelectedIndexChanged-=new EventHandler(editingControl_SelectedIndexChanged);
取消事件绑定,等下一次点击再重新绑定