C# 中DataGridView和ListView闪烁问题的解决方法

方法一
首先定义类,将此类放在datagridview或ListView所在的窗体类外面,然后代码如下,

<span style="font-family:Microsoft YaHei;font-size:18px;">// <summary>
/// 双缓冲DataGridView,解决闪烁
/// 使用方法:在DataGridView所在窗体的InitializeComponent方法中更改控件类型实例化语句将
/// this.dataGridView1 = new System.Windows.Forms.DataGridView(); 屏蔽掉,添加下面这句即可
/// this.dataGridView1 = new DoubleBufferListView();
/// </summary>
class DoubleBufferDataGridView : DataGridView
{
public DoubleBufferDataGridView()
{
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
//UpdateStatus.Continue;
UpdateStyles();
}
}

/// <summary>
/// 双缓冲ListView ,解决闪烁
/// 使用方法是在ListView 所在窗体的InitializeComponent方法中,更改控件类型实例化语句将
/// this.listView1 = new System.Windows.Forms.ListView(); 屏蔽掉, 添加下面语句即可
/// this.listView1 = new DoubleBufferListView();
/// </summary>
class DoubleBufferListView : ListView
{
public DoubleBufferListView()
{
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
UpdateStyles();
}
}</span>
方法二
直接写一个扩展方法,使用反射,直接上代码,将此类定义给DataGirdView或ListView所在的窗体类外面即可
<span style="font-family:Microsoft YaHei;font-size:18px;">public static class DoubleBufferDataGridView
{
/// <summary>
/// 双缓冲,解决闪烁问题
/// </summary>
/// <param name="dgv"></param>
/// <param name="flag"></param>
public static void DoubleBufferedDataGirdView(this DataGridView dgv, bool flag)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, flag, null);
}
}

public static class DoubleBufferListView
{
/// <summary>
/// 双缓冲,解决闪烁问题
/// </summary>
/// <param name="lv"></param>
/// <param name="flag"></param>
public static void DoubleBufferedListView(this ListView lv, bool flag)
{
Type lvType = lv.GetType();
PropertyInfo pi = lvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(lv, flag, null);
}

}</span>

//调用方法
<span style="font-family:Microsoft YaHei;font-size:18px;">public Form1()
{
InitializeComponent();
DataGridView1.DoubleBufferedDataGirdView(true);
}</span>

其实很简单的,一看代码就能明白,使用双缓冲解决闪烁问题

---------------------
作者:ching126
来源:CSDN
原文:https://blog.csdn.net/chenhongwu666/article/details/43305503
版权声明:本文为博主原创文章,转载请附上博文链接!

C# 中DataGridView和ListView闪烁问题的解决方法的更多相关文章

  1. WAMP中phpMyAdmin登陆不了问题的解决方法

    WAMP中phpMyAdmin登陆不了问题的解决方法

  2. 问题-[Access]“无法打开工作组信息文件中的表 'MSysAccounts'”的问题的解决方法

    问题现象:ado.net oledb方式访问Access数据库文件时报错“无法打开工作组信息文件中的表 'MSysAccounts'”的问题的解决方法  问题处理:1.数据库名称不能命名为:Syste ...

  3. PHP开发中常见的安全问题详解和解决方法(如Sql注入、CSRF、Xss、CC等

    页面导航: 首页 → 网络编程 → PHP编程 → php技巧 → 正文内容 PHP安全 PHP开发中常见的安全问题详解和解决方法(如Sql注入.CSRF.Xss.CC等) 作者: 字体:[增加 减小 ...

  4. jquery ui中 accordion的问题及我的解决方法

    原文:jquery ui中 accordion的问题及我的解决方法 jquery有一套所谓的ui组件,很不错的.如果有兴趣的朋友,可以参考http://jqueryui.com/ 但其中的accord ...

  5. ASP.NET MVC中对Model进行分步验证的解决方法

    原文:ASP.NET MVC中对Model进行分步验证的解决方法 在我之前的文章:ASP.NET MVC2.0结合WF4.0实现用户多步注册流程中将一个用户的注册分成了四步,而这四个步骤都是在完善一个 ...

  6. JS网站当前日期在IE9、Chrome和FireFox中年份显示为113年的解决方法 getFullYear();

    JS网站当前日期在IE9.Chrome和FireFox中年份显示为113年的解决方法 getFullYear();

  7. 关于CUDA C 项目中“ error C2059: 语法错误:“<” ”问题的解决方法

    该问题的关键在于理解CUDA项目中C\C++文件需要由c++编译器进行编译,而CUDA C的源文件需要由CUDA的编译器nvcc.exe进行编译. 发生该语法错误的原因是cu文件被C++编译器所编译, ...

  8. ios UITableView中Cell重用机制导致内容重复解决方法

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...

  9. 如何自定义JSTL标签与SpringMVC 标签的属性中套JSTL标签报错的解决方法

    如何自定义JSTL标签 1.创建一个类,从SimpleTagSupport继承 A) 通过继承可以获得当前JSP页面上的对象,如JspContext I) 实际上可以强转为PageContext II ...

随机推荐

  1. mysql无法安装,报 The older version of MySQL Installer - Community cannot be removed. Contact your technical support group

    解决办法: C:\Users\Administrator>msiexec /i C:\Users\Administrator\Downloads\mysql-installer-web-comm ...

  2. FCC JS基础算法题(5):Return Largest Numbers in Arrays(找出多个数组中的最大数)

    题目描述: 找出多个数组中的最大数右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组.提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组 ...

  3. 初涉FlaskWeb开发----基础篇

    1.web程序运行的基本流程 {客户端发送请求 <-----> 服务器返回响应} 2.使用框架可以降低开发难度,提高开发效率. 3.Flask框架的基本认识: 特点:用Python语言实现 ...

  4. 字符串与NUll的拼接问题

    今天做项目,浏览器向后台传值的时候,碰到一个问题,声明变量的时候为null时,首次加载会报错.但是初始化一次后,就正常传值了,摸索了半天,终于找到问题所在.在此记录一下,谨记. 现在说说情况,我在JS ...

  5. 正则表达式andJS内存空间详细图解

    http://www.runoob.com/js/js-regexp.html https://blog.csdn.net/pingfan592/article/details/55189622

  6. 08_java基础知识——方法重载

    一.自变量顺序不同 package com.huawei.test.java04; /** * This is Description * * @author 王明飞 * @date 2018/08/ ...

  7. spring cloud_1_mm_eureka2 eureka集群

    一个eureka会出现单点故障 这里整两个 eureka--1 application.yml: #注册中心端口 server: port: 8888 #唯一标示eureka注册中心 方便互相识别 e ...

  8. 如何配置Tomcat以使用Apache httpd?

    How to Connect Tomcat 6 to Apache HTTP Server 2 Tomcat can be run as a standalone server. Tomcat can ...

  9. 微信小程序测试策略

    一.测试前准备(环境搭建) 1.前端页面 微信Web开发者工具安装.授权测试用的微信号可预览和调试小程序... 可参考此文: 微信Web开发者工具-下载.安装和使用图解 2.管理后台 配置内网测试服务 ...

  10. ubuntu16.04如何安装floodlight并且连接eclipse

    按顺序走,亲测没出错.用的是Luna R版本的eclipse https://floodlight.atlassian.net/wiki/spaces/floodlightcontroller/pag ...