如何提高DataGridView的绘画性能?

发布于 2024-10-03 14:23:57 字数 407 浏览 11 评论 0原文

(对不起,英语不好)

重新绘制时,我在 DataGridView 的性能方面遇到了问题。

我正在使用 DataGridView 显示来自外部应用程序流的日志。来自流的消息以高频率进入(小于 1 毫秒)。如果我在每条新消息到来时立即向 DataGridView 添加新行,则在下一条消息到来之前,DataGridView 没有时间重新绘制自身。

一种可能的解决方案是使用队列来收集消息,并每 100 毫秒使用队列中的消息重新绘制 DataGridView。这很好,但是 DataGridView 在自动滚动到最后一行时会闪烁。 (平滑滚动已禁用)

您能帮助我提高 DataGridView 性能吗?

(sorry for bad English)

I have a big problem with performance of DataGridView when it re-paints.

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.

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)

Can you help me to improve DataGridView performance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一城柳絮吹成雪 2024-10-10 14:23:57

我最近遇到了 DataGridView 的一些缓慢问题,解决方案是以下代码

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() 即可。希望有帮助。

编辑:我可能已经把这个关闭了,但我现在无法搜索原始代码,所以这只是为了强调代码不是我的。

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);
}

It turns double buffering on for DataGridView objects. Just call DoubleBuffered() on your DGV. Hope it helps.

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

ぃ双果 2024-10-10 14:23:57

您是否为网格视图启用了双缓冲?

看看
DataGridView 在一个屏幕上的重绘性能非常糟糕我的两个屏幕

如果您还没有想出一些想法)

Have you enabled double buffering for the grid view?

have a look at
Horrible redraw performance of the DataGridView on one of my two screens

if you haven't already for some ideas

乖乖哒 2024-10-10 14:23:57

没有反射的干净解决方案是:

public class DataGridViewDoubleBuffered : DataGridView
{
   public DataGridViewDoubleBuffered()
   {
       DoubleBuffered = true;
   }
}

然后转到 myForm.designer.cs 并将类型从 DataGridView 更改为 DataGridViewDoubleBuffered 。

Clean Solution without reflection is:

public class DataGridViewDoubleBuffered : DataGridView
{
   public DataGridViewDoubleBuffered()
   {
       DoubleBuffered = true;
   }
}

Then go to myForm.designer.cs and change a type from DataGridView to DataGridViewDoubleBuffered .

美人如玉 2024-10-10 14:23:57

处理大量数据时,DataGridView 控件可能会消耗大量内存,除非您小心使用它。在内存有限的客户端上,您可以通过避免使用内存成本较高的功能来避免部分开销。

您还可以使用虚拟模式自行管理部分或全部数据维护和检索任务,以便为您的场景自定义内存使用情况。更多详情您可以访问dapfor。 com

When working with large amounts of data, the DataGridView control can consume a large amount of memory in overhead, unless you use it carefully. On clients with limited memory, you can avoid some of this overhead by avoiding features that have a high memory cost.

You can also manage some or all of the data maintenance and retrieval tasks yourself using virtual mode in order to customize the memory usage for your scenario. More detail you can visit dapfor. com

羁〃客ぐ 2024-10-10 14:23:57

我使用这个解决方案并修复了锯齿。

使用反射,因此也在代码中导入它

using System.Reflection;

typeof(DataGridView).InvokeMember("DoubleBuffered",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty,
null,this.dataGridView1,new object[] { true });

i use this solution and saw bit fixed.

Reflection is used so import this too in code

using System.Reflection;

typeof(DataGridView).InvokeMember("DoubleBuffered",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty,
null,this.dataGridView1,new object[] { true });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文