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
来自:
当DataGridView达到一定规模时,会出现滚动缓慢及闪动现象。启用双缓冲能有效减少内部调用次数,减轻CPU负担,提高滚动速度。通过反射设置隐藏属性DoubleBuffered为true即可实现。
1413

被折叠的 条评论
为什么被折叠?



