开发者

BindingSource and DataGridView default current position

Does the BindingSource have an automatic default current position? I have an 开发者_运维知识库event handler on CurrentCellChanged event and it seems to be firing twice. I am programatically setting the starting position using the BindingSource Find method at that works but before im setting that starting position, the CurrentCellChanged is already firing and the initial selected cell is column 0 row 0. When you create a BindingSource is it already setting the Current property?


MSDN for DataGridView.CurrentCell Property mentions the default CurrentCell property value is the first cell in the first row (or null if there are no cells in the DGV).

Setting this default would fire your CurrentCellChanged event, explaining why you're seeing the event for cell 0, 0.


I'm pretty sure what you are seeing is the DataGridView firing its various selection events (CurrentCellChanged, SelectionChanged etc...) during the databinding process. Because you have attached an eventhandler to one of these events it fires.

The way around this is to attach an eventhandler to the DataGridView's DataBindingComplete and attach your CurrentCellChanged handler there.

// 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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜