数据源更改时如何防止DataGridView自动滚动? [英] How do I prevent a DataGridView from autoscrolling when the datasource changes?

查看:345
本文介绍了数据源更改时如何防止DataGridView自动滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过( http ://brainof-dave.blogspot.com/2007/08/turning-off-auto-scrolling-in-bound.html )在DataTable的RowChanged事件中,DataTable是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.

基本上,我有一个DataGridView和BindingSource是DataSource。 BindingSource的DataSource是包含DataTable的DataView。每一行的数据每次更改时,DataGridView会滚动回到顶部。有一个简单的解决方案吗?

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?

推荐答案

看起来我发现它: http://seewinapp.blogspot.com/2005/09/is-your-autoscroll-too-auto .html

我覆盖了DataTable上的RowChanged事件,存储了FirstDisplayedScrollingRowIndex,以该索引为参数调用了一个委托方法,然后重新设置FirstDisplayedScrollingRowIndex到委托方法中的参数。事实证明,自动滚动不会发生,直到所有的事件被触发之后,所以试图在一个事件中进行破解是没有用的。代表工作,因为它在事件后被调用。

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.

这篇关于数据源更改时如何防止DataGridView自动滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆