datagridview 出错问题
来源:互联网 发布:淘宝龙瞎多少钱 编辑:程序博客网 时间:2024/06/05 19:37
C#代码
Fixing a slow scrolling DataGridView
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
- datagridview 出错问题
- c# DataGridView 加入Combobox下拉框 问题 输入数字零出错
- c# DataGridView 加入Combobox下拉框 问题 输入数字零出错
- c# DataGridView 加入Combobox下拉框 问题 输入数字零出错
- Datagridview解决列中间有空值时的排序出错问题
- Datagridview半角问题
- [VB.NET]datagridview问题
- [VB.NET]datagridview 问题
- DataGridView 存放图片问题
- datagridview显示问题
- datagridview涉及计算问题
- DataGridView绑定问题
- dataGridView 遇到问题
- datagridview 列排序问题
- Winform datagridview绑定问题
- datagridview 列排序问题
- 关于DataGridView控件的问题
- DataGridView最后一行 的问题
- wince 隐藏任务栏
- 关于stuts2文件上传出错java.lang.NoSuchMethodException: com.cn.file.upload.UploadFile.uploadfile()
- python中的迭代器和生成器(一)——迭代器
- oracle中如何查看数据库服务器的ip地址
- 【索引】String
- datagridview 出错问题
- hadoop 集群及hbase集群的pid文件存放位置
- nil 和 releasse 作用
- 自定义语言的实现——解释器模式
- HDU 3001 Travelling ( 状态压缩 )
- hdu1875
- python协程入门(greenlet)
- jsp页面声明对jstl的支持
- 修改standby库的sys用户的密码导致DG无法传输日志及切换