首页 文章

BindingSource和DataGridView默认当前位置

提问于
浏览
1

BindingSource是否具有自动默认当前位置?我在CurrentCellChanged事件上有一个事件处理程序,它似乎是两次触发 . 我使用BindingSource Find方法以编程方式设置起始位置,但在设置起始位置之前,CurrentCellChanged已经触发,初始选定的单元格是第0列第0行 . 当您创建BindingSource时,它已经设置了Current属性?

2 回答

  • 1

    DataGridView.CurrentCell Property的MSDN提到默认的CurrentCell属性值是第一行中的第一个单元格(如果DGV中没有单元格,则为null) .

    设置此默认值将触发CurrentCellChanged事件,解释您为什么看到单元格0,0的事件 .

  • 1

    我很确定你所看到的是DataGridView在数据绑定过程中触发了各种选择事件(CurrentCellChanged,SelectionChanged等...) . 因为您已将事件处理程序附加到其中一个事件中,所以它会触发 .

    解决这个问题的方法是将一个eventhandler附加到DataGridView的DataBindingComplete,并在那里附加你的CurrentCellChanged处理程序 .

    // Attach the event in the form's constructor
    this.dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
    
    // And in the eventhandler, attach to the CurrentCellChanged event.
    void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        dataGridView1.CurrentCellChanged += new EventHandler(dataGridView1_CurrentCellChanged);
    }
    

相关问题