当数据源更改时,如何防止 DataGridView 自动滚动?
我尝试了这个(http:// Brainof-dave.blogspot.com/2007/08/turning-off-auto-scrolling-in-bound.html) 在作为 DataGridView 的数据源的 DataTable 上的“RowChanged”事件中,但没有用。
基本上,我有一个带有 BindingSource 的 DataGridView,因为它是 DataSource。 BindingSource 的DataSource 是一个包含DataTable 的DataView。每当其中一行中的数据发生更改时,DataGridView 就会滚动回顶部。有一个简单的解决办法吗?
I tried this (http://brainof-dave.blogspot.com/2007/08/turning-off-auto-scrolling-in-bound.html) in the "RowChanged" event on the DataTable that is the data source for the DataGridView, but to no avail.
Basically, I have a DataGridView with a BindingSource as it's DataSource. The BindingSource's DataSource is a DataView that contains a DataTable. Every time data in one of the rows changes, the DataGridView scrolls back to the top. Is there a simple fix for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来我找到了它:http://seewinapp。 blogspot.com/2005/09/is-your-autoscroll-too-auto.html
我覆盖了 DataTable 上的 RowChanged 事件,存储了 FirstDisplayedScrollingRowIndex,使用该索引作为参数调用委托方法,然后重置委托方法内该参数的 FirstDisplayedScrollingRowIndex。事实证明,直到所有事件都被触发后,自动滚动才会发生,因此尝试在事件中破解它是没有用的。该委托之所以有效,是因为它是在事件发生后调用的。
Looks like I found it: http://seewinapp.blogspot.com/2005/09/is-your-autoscroll-too-auto.html
I overrode the RowChanged event on the DataTable, stored the FirstDisplayedScrollingRowIndex, invoked a delegate method with that index as the argument, and then reset the FirstDisplayedScrollingRowIndex to that argument inside the delegate method. It turns out that auto-scroll doesn't occur until after all events have been fired, so it's useless to try to hack it inside an event. The delegate works because it is invoked after the events.
这是经过测试的代码,可在更改数据源后恢复 RowIndex。这还会恢复排序顺序和最后一个单元格位置。语言:C#7.0。
这是我在网络搜索的帮助下亲自编写的代码。
Here is tested code that restores the RowIndex after changing the datasource. This also restores the sort order and last cell position. Language: C# 7.0.
This is code I wrote personally, with some help from web searches.