65.9K
CodeProject is changing. Read more.
Home

Double Buffering a DataGridview

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.97/5 (14 votes)

Oct 23, 2013

CPOL
viewsIcon

61690

Two ways to double buffer a DataGridView

Introduction

Recently, I had a Winforms projects that required me to display data approximately 1000 rows to a DataGridView control. Once added, some logic was applied to each row to determine the color of the row. There was no problem until the user began scrolling through the information. It caused the rows to flicker.

Using the Code

As the DoubleBuffered property of the DataGridView control is hidden, you have to either create a custom class so that you can set the property.

public partial class myDataGridView : DataGridView
{
    public myDataGridView()
    {
        InitializeComponent();
        DoubleBuffered = true;
    }    
}

Or you could invoke the property using the following code snippet below where DataGridViewControlName is the name of your DataGridView control that you are using in your code.

//Set Double buffering on the Grid using reflection and the bindingflags enum.
typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | 
BindingFlags.Instance | BindingFlags.SetProperty, null, 
DataGridViewControlName, new object[] { true });

History

  • Original version