当水平滚动时如何防止DataGridView闪烁? [英] How to prevent DataGridView from flickering when scrolling horizontally?

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

问题描述

我正在使用Windows窗体C#。





如屏幕截图所示,我有一个具有用户控件,制表符控件和DataGridView(30行和17列)的窗体。我从SQL Server中读取数据以填充DataGrdiView。



问题:



当我水平滚动时, DataGridView 闪烁很多。但是垂直滚动可以完美无瑕疵。



我看了一下



请注意,表单还有 DoubleBuffering ,但这将传播到任何嵌入式控件!


I am using windows forms C#.

As shown in the screen shot, I have a Form which has a user control, a tab control and a DataGridView (30 rows and 17 columns). I read data from SQL Server to fill the DataGrdiView.

The issue:

When I scroll horizontally the DataGridView flickers a lot. However scrolling vertically works perfect with no flickering.

I had a look here, here, here and here but none of them related to my issue.

Anyone knows any solution to prevent DataGridView from flickering when scrolling horizontally.

解决方案

All you need is to use a DoubleBuffered DataGridview subclass:

class DBDataGridView : DataGridView
{
    public DBDataGridView() { DoubleBuffered = true; }
}

It is also possible to inject double-buffering into a normal out-of-the-box control, but I prefer to have a class of my own as this is extensible in other way as well..

I have expanded the class by a public property to allow turning DoubleBuffering on and off:

public class DBDataGridView : DataGridView
{
    public bool DblBuf {
        get { return DoubleBuffered; }
        set { DoubleBuffered = value; } }

    public DBDataGridView()
    {
        DoubleBuffered = true;
    }
}

And tested it with a load of 200 columns and 2000 rows. The difference is obvious; while vertical scrolling did work without horizontal scrolling needs DoubleBuffering..

Note that the Form also has DoubleBuffering on, but that will not propagate to any embedded controls!

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

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