// 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);
}
2 回答
DataGridView.CurrentCell Property的MSDN提到默认的CurrentCell属性值是第一行中的第一个单元格(如果DGV中没有单元格,则为null) .
设置此默认值将触发CurrentCellChanged事件,解释您为什么看到单元格0,0的事件 .
我很确定你所看到的是DataGridView在数据绑定过程中触发了各种选择事件(CurrentCellChanged,SelectionChanged等...) . 因为您已将事件处理程序附加到其中一个事件中,所以它会触发 .
解决这个问题的方法是将一个eventhandler附加到DataGridView的DataBindingComplete,并在那里附加你的CurrentCellChanged处理程序 .