datagridview 出错问题

C#代码


Fixing a slow scrolling DataGridView

On October 25, 2009, Posted by Jakob, In C#, By .NET,C#,controls,DataGridView,performance,Scroll,VS2008 , With 49 Comments

Whenever your C#/.NET DataGridView reaches a certain size, it tends to get really slow to scroll. Depending on the speed of your computer this may be more or less noticeable. In an application i did for a client this became a real problem due to a combination of lots of DataGridView cells and fairly slow computers.
Luckily the solution turned out to be simple…

Turn on double buffering

Turning on double buffering seems to solve the problem. Normally double buffering would only help reduce flickering, since painting is being done to an off-screen buffer, but for the DataGridView it also significantly reduces the amount of functions being called internally in the DataGridView – thus reducing processor load and increasing speed. (statistics gathered with the Eqatec Tracer)

My DataGridView doesn’t have a DoubleBuffered property !?!?

For some reason Microsoft has decided to hide the DoubleBuffered property from DataGridView. Luckily you can set it anyway with reflection.

public static class ExtensionMethods
{
    public static void DoubleBuffered(this DataGridView dgv, bool setting)
    {
        Type dgvType = dgv.GetType();
        PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
            BindingFlags.Instance | BindingFlags.NonPublic);
        pi.SetValue(dgv, setting, null);
    }
}

Just drop the above class into your project somewhere, or add the function to your existing extension methods.
The extension method allows you to set the DoubleBuffered property on your DataGridView in the following manner:

dataGridView1.DoubleBuffered(true);

You now have a smooth scrolling DataGridView :-)



以下是反射获取双缓存代码:

           public Form()
        {
            //设置窗体的双缓冲
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
            this.UpdateStyles();
            
            InitializeComponent();

 

            //利用反射设置DataGridView的双缓冲
            Type dgvType = this.dataGridView.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
                BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(this.dataGridView, true, null);
        }


For anyone needing this in VB.Net:

‘Code Below
Imports System
Imports System.Reflection
Imports System.Windows.Forms

Public Module ExtensionMethods
_
Public Sub DoubleBuffered(ByVal dgv As DataGridView, ByVal setting As Boolean)
Dim dgvType As Type = dgv.[GetType]()
Dim pi As PropertyInfo = dgvType.GetProperty(“DoubleBuffered”, BindingFlags.Instance Or BindingFlags.NonPublic)
pi.SetValue(dgv, setting, Nothing)
End Sub

End Module

‘End Code


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值