每个像素滚动DataGridView [英] Scrolling DataGridView per pixel

查看:151
本文介绍了每个像素滚动DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个.NET 4 WinForms应用程序,它从数据库读取数据并在DGV中显示行。但是,行数大于可以一次适合屏幕的最大行数。要显示所有数据,我需要自动滚动DGV直到最后一行,然后更新数据源,以刷新DGV。



我发现很容易只需将 FirstDisplayedScrollingRowIndex 增加一个即可。
然而,滚动是太尖锐了。我想要滚动滚动。



为此,我已经尝试直接调用 ScrollRows 方法。这个方法不是公开的,所以我不得不像这样使用Reflection:

  var scrollRows = dgv.GetType()GetMethod( ScrollRows,BindingFlags.Instance | BindingFlags.NonPublic); 
scrollRows.Invoke(dgv,new object [] {1,-1,ScrollEventType.SmallIncrement});

我从 timer_Tick 方法中调用此代码每20ms触发一次。



DGV平滑滚动,但不会在开头显示的那一行下方显示任何行。



有什么办法可以让它滚动顺利地显示数据吗?

解决方案

我有降低了对DGV的需求,因此减少了对这种方法的需求。我已经创建了一个FlowLayoutPanel,动态创建面板,以显示所需的数据,以我想要的方式设计。



可能有一种方法可以顺利滚动DataGridView这可以通过分析DataGridView代码的.NET反射来实现,但是再次,我发现了一个可以接受的替代方法,我正在使用它。



有一种方法来实现平滑通过实际将其放置在带滚动条的面板上来滚动DGV。这样,滚动面板也会滚动DGV。这样对我来说并不方便,因为DGV标头会从屏幕上滚动,我需要它们始终可见。


I am making a .NET 4 WinForms application which reads data from a database and displays rows in a DGV. However the number of rows is larger than maximum number of rows that can fit on screen at once. To display all data, I need to scroll the DGV automatically until the last row and update the data source after that in order to refresh the DGV.

I found it's easy to do that by simply incrementing FirstDisplayedScrollingRowIndex by one. However the scrolling is too sharp. I would like it to scroll smoothly.

To do that, I have tried calling ScrollRows method directly. That method is not public so I had to use Reflection like this:

var scrollRows = dgv.GetType().GetMethod("ScrollRows", BindingFlags.Instance | BindingFlags.NonPublic);
scrollRows.Invoke(dgv, new object[] { 1, -1, ScrollEventType.SmallIncrement });

I call this code from a timer_Tick method which is triggered every 20 ms.

The DGV scrolls smoothly but it doesn't draw any row below those which were visible at the beginning.

Is there any way I can make it scroll smoothly AND display data properly?

解决方案

I have dropped the need for DGV and therefore for this kind of approach. I have created a FlowLayoutPanel with Panels created dynamically to display the required data, designed the way I wanted it to be.

There is probably a way to perform smooth scrolling of DataGridView which could be accomplished by analyzing .NET reflection of DataGridView code, but again, I found an acceptable alternative and I'm using it.

There is a way to accomplish smooth scrolling of DGV by actually placing it on a panel with a scrollbar. That way, scrolling a panel scrolls the DGV too. This way wasn't convenient for me because DGV headers would get scrolled out of screen and I needed them to be visible at all times.

这篇关于每个像素滚动DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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