7

I am using windows forms C#.

Screen Shot

As shown in the screen shot, I have a Form which has a user control, a tab control and a DataGridView (30 rows and 17 columns). I read data from SQL Server to fill the DataGrdiView.

The issue:

When I scroll horizontally the DataGridView flickers a lot. However scrolling vertically works perfect with no flickering.

I had a look here, here, here and here but none of them related to my issue.

Anyone knows any solution to prevent DataGridView from flickering when scrolling horizontally.

Community
  • 1
  • 1
Sam
  • 281
  • 1
  • 4
  • 13
  • 1
    Did you try to enable the double buffering in the form? – Carles Jan 27 '17 at 12:12
  • I think its because of events, whcih events do you use for grid? – kgzdev Jan 27 '17 at 12:12
  • @Carles . no. can you please tell me how to do it, I am new to C# – Sam Jan 27 '17 at 12:26
  • @ Ikram Turgunbaev . I do not use any events, i just fill the DGV and thats all. – Sam Jan 27 '17 at 12:26
  • 2
    @Carles: DoublBuffering the Form will only help the form, not any of the embedded controls. – TaW Jan 27 '17 at 13:48
  • @TaW Double buffering an object acts over that object and any children contained. I've been using this over 10 years. https://msdn.microsoft.com/en-us/library/b367a457(v=vs.110).aspx – Carles Jan 30 '17 at 07:42
  • @Sam there is a boolean property on each form called DoubleFuffered. This must be set to True. If enabled, then, the redrawing is made in memory and then transferred to the screen, instead of being done in "real time" in screen. It is widely used in application with high graphic load (spreadsheets, drawing tools, games, etc). – Carles Jan 30 '17 at 07:46
  • 1
    I don't think so. Read this closely: _Standard Windows Forms controls are double buffered by default. You can enable default double buffering in your forms __and authored controls__ in two ways. You can either set the DoubleBuffered property to true, or you can call the SetStyle method to set the OptimizedDoubleBuffer flag to true. Both methods will enable default double buffering for your form __or__ control and provide flicker-free graphics rendering._ Note it say 'authored' controls (i.e.subclassed) not 'nested'. – TaW Jan 30 '17 at 07:49

4 Answers4

24

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.

Ramgy Borja
  • 2,330
  • 2
  • 19
  • 40
15

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..

enter image description here

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

TaW
  • 53,122
  • 8
  • 69
  • 111
  • 1
    Is there any other way? I am looking for something like `dgv.DoubleBuffered = true;`. – Ganesh Kamath - 'Code Frenzy' Apr 05 '17 at 08:44
  • 1
    I have updated the answer to include a `new DoubleBuffered` property. – TaW Apr 05 '17 at 14:09
  • @TaW This is not related to the question however my inquiry is about your method of answering the question. What software did you use to create your Gif? I've posted my own Gifs when answering questions however creating of the Gifs was time consuming and not perfect. What is your method(s) in creating your Gifs? – Code Novice Feb 24 '19 at 17:09
  • 1
    @Code: I use ScreenToGif either 2.1 or 2.9. Make sure to play with the options. 'Include mouse' & 'constant framerate'. – TaW Feb 24 '19 at 17:46
9

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;
Khalil
  • 187
  • 2
  • 10
0

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.

Code Novice
  • 2,043
  • 1
  • 20
  • 44