How to prevent DataGridView from flickering when scrolling horizontally?

前端 未结 4 691
闹比i
闹比i 2020-11-28 14:03

I am using windows forms C#.

\"Screen

As shown in the screen shot, I have a Form which has a use

相关标签:
4条回答
  • 2020-11-28 14:23

    In your 'FormLoad' function just enter this line of code.

    yourDataGridView.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(yourDataGridView, true, null);
    

    and import BindingFlags by writing below line on top.

    using System.Reflection;
    
    0 讨论(0)
  • 2020-11-28 14:26

    All you need is to use a DoubleBuffered DataGridview subclass:

    class DBDataGridView : DataGridView
    {
        public DBDataGridView() { DoubleBuffered = true; }
    }
    

    It is also possible to inject double-buffering into a normal out-of-the-box control, but I prefer to have a class of my own as this is extensible in other ways as well..

    I have expanded the class by a public property to allow turning DoubleBuffering on and off..:

    public class DBDataGridView : DataGridView
    {
        public new bool DoubleBuffered
        {
            get { return base.DoubleBuffered; }
            set { base.DoubleBuffered = value; }
        }
    
        public DBDataGridView()
        {
            DoubleBuffered = true;
        }
    }
    

    ..and tested it with a load of 200 columns and 2000 rows. The difference is obvious; while vertical scrolling did work without horizontal scrolling needs DoubleBuffering..

    Note that the Form also has a DoubleBuffering property, but that will not propagate to any embedded controls!

    Or you can use a function like this

    0 讨论(0)
  • 2020-11-28 14:34

    In case anybody wanted to see this in Visual Basic.

    Public Class DBDataGridView
        Inherits DataGridView
    
        Public Sub New()
            MyBase.New()
            DoubleBuffered = True
        End Sub
    
    End Class
    

    This worked great.

    0 讨论(0)
  • 2020-11-28 14:41

    use this class

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

    and enter this code.

    this.dataGridView1.DoubleBuffered(true);
    

    enjoy.

    0 讨论(0)
提交回复
热议问题