DataGridView滚动慢的解决方法

当DataGridView达到一定大小的时候,拖动滚动条就会非常慢,出现让人难以忍受的闪动。
即便只有100行,每行30列。
解决方法是启用DataGridView的双缓冲。

首先导入命名空间using System.Reflection;

原文:

Fixing a slow scrolling DataGridView

Filed in C# on Oct.25, 2009

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.

 
  1. public static class ExtensionMethods

  2. {

  3. public static void DoubleBuffered(this DataGridView dgv, bool setting)

  4. {

  5. Type dgvType = dgv.GetType();

  6. PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",

  7. BindingFlags.Instance | BindingFlags.NonPublic);

  8. pi.SetValue(dgv, setting, null);

  9. }

  10. }

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 :-)

来自:

http://h2appy.blog.51cto.com/609721/289076

http://bitmatic.com/c/fixing-a-slow-scrolling-datagridview

猜你喜欢

转载自blog.csdn.net/jasonhongcn/article/details/84869041