如何连续更改datagridview中的同一组合框?

时间:2018-08-31 15:42:00

标签: c#

我在datagridview中有一个组合框,在处理第一个事件时,它可以正常工作,当我从另一行的组合框中选择值时,它也可以正常工作,但问题是当我从同一框中的同一组合框中选择值时排两次。在第二个事件处理中,它不会转到“ dataGridView1_EditingControlShowing” 方法,而是直接转到“ combo_SelectedIndexChanged” ,它会导致 System.NullReferenceException:'Object引用未设置为对象的实例。'

 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        combo = e.Control as ComboBox;
        if(combo != null)
        {
            combo.SelectedIndexChanged -= new EventHandler(combo_SelectedIndexChanged);
            combo.SelectedIndexChanged += combo_SelectedIndexChanged;
        }
    }

    public void combo_SelectedIndexChanged(object sender, EventArgs e)
    {
            selected = (sender as ComboBox).SelectedItem.ToString();
            LoadData(selected);
    }

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要转到form.designer.cs文件,并更新所选索引已更改的组合框的指针,以指向dataGridView1_EditingControlShowing

例如           this.comboBox1.SelectedIndexChanged + =新的System.EventHandler(this.dataGridView1_EditingControlShowing);