"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Prevent Flickering When Frequently Updating a DataGridView?

How to Prevent Flickering When Frequently Updating a DataGridView?

Posted on 2025-03-23
Browse:933

How to Prevent Flickering When Frequently Updating a DataGridView?

Frequently update the DataGridView without flashing

question:

Refresh DataGridView in real time, especially when the update rate is high and the number of cells is large, visual defects such as flickering and lag are prone to occur.

analyze:

Double buffering technology solves these problems by using off-screen buffers.

Solution:

Enable the double buffering function of DataGridView through reflection or subclassing methods.

Subclassing method

Create a subclass of DataGridView, expose the DoubleBuffered property:

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

    public DBDataGridView()
    {
        DoubleBuffered = true;
    }
}

Add this class to the project and set DoubleBuffering to true.

Reflection method

Set DoubleBuffering programmatically using reflection:

using System.Reflection;

static void SetDoubleBuffer(Control ctl, bool DoubleBuffered)
{
    typeof(Control).InvokeMember("DoubleBuffered",
        BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty,
        null, ctl, new object[] { DoubleBuffered });
}

Call SetDoubleBuffer to switch the DoubleBuffering of DataGridView.

By enabling DoubleBuffering, DataGridView will draw updates using off-screen buffers, reducing flickering and stuttering during frequent updates.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3