BindingSource 和 DataGridView 默认当前位置

发布于 2024-11-03 02:03:17 字数 216 浏览 3 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2024-11-10 02:03:17

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

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

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.

心头的小情儿 2024-11-10 02:03:17

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

解决此问题的方法是将事件处理程序附加到 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文