当前位置:首页 >.NET > 正文内容

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

大滑稽10年前 (2015-10-30).NET1433

方法一

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

// <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();
    }
}

方法二
直接写一个扩展方法,使用反射,直接上代码,将此类定义给DataGirdView或ListView所在的窗体类外面即可

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);
    }

}


扫描二维码推送至手机访问。

版权声明:本文由第★一★次发布,如需转载请注明出处。

本文链接:http://wpers.net/post/13.html

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

Cbo控件数据源绑定

 //Cbo控件数据源绑定DataTable DtType = noteType.GetTypeList("");         ...

C#遍历控件的方法

首先,要想遍历,就必须找到你想找的表单里面的所有控件,然后一个个的逐一比对,当找到了你需要的控件的时候,再做你需要的操作。1、foreach方法foreach (Control control in ...

Linq读写XML

         private List<News> GetNews(string html)    &n...

c#中分页显示数据

     //c#中分页显示数据    public partial class Form1 : Form    {  ...

C#获得程序集

 //获得程序集System.Reflection.Assembly assem = System.Reflection.Assembly.GetExecutingAssembly();...

获取Color的几种方式

//获取Color的几种方式Color.FromKnownColor(KnownColor.ControlLight);Color.FromArgb(int r,int g,int b);Color.FromArgb(int a,int r...