如何提高DataGridView的绘制性能? [英] How to improve painting performance of DataGridView?
问题描述
(抱歉英语不好)
当 DataGridView
重新绘制时,它的性能有一个大问题.
I have a big problem with performance of DataGridView
when it re-paints.
我正在使用 DataGridView
来显示来自外部应用程序流的日志.来自流的消息以高频率(小于 1 毫秒)传入.如果我在每条新消息到来时立即向 DataGridView
添加新行,DataGridView
就没有时间在下一条消息到来之前重新绘制自己.
I'm using a DataGridView
to show logs from an external application stream. Messages from the stream come in with a high frequency (less than 1 ms). If I add new row to the DataGridView
immediately when each new message comes, the DataGridView
doesn't have time to re-paint itself before the next message comes.
一种可能的解决方案是使用队列来收集消息并每 100 毫秒使用队列中的消息重新绘制 DataGridView
.这很好,但是 DataGridView
在自动滚动到最后一行时会闪烁.(禁用平滑滚动)
A possible solution is to use a queue to collect messages and re-paint DataGridView
every 100 ms with messages from queue. This is good but the DataGridView
blinks when it auto-scrolls to the last row. (Smooth scroll is disabled)
你能帮我提高 DataGridView
的性能吗?
Can you help me to improve DataGridView
performance?
推荐答案
我最近在使用 DataGridView
时遇到了一些缓慢的问题,解决方案是以下代码
I recently had some slowness issues with DataGridView
and the solution was the following code
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);
}
它为 DataGridView
对象开启双缓冲.只需在您的 DGV 上调用 DoubleBuffered()
.希望有帮助.
It turns double buffering on for DataGridView
objects. Just call DoubleBuffered()
on your DGV. Hope it helps.
我可能已经把它弄掉了,但我现在无法搜索原件,所以这只是为了强调代码不是我的.
I might've gotten this off SO, but I can't search for the original right now so this is just to emphasize that the code isn't mine.
这篇关于如何提高DataGridView的绘制性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!