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