.net 如何提高DataGridView的绘制性能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4255148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:58:02  来源:igfitidea点击:

How to improve painting performance of DataGridView?

.netwinformsdatagridviewrepaintpaint

提问by Yuriy

(sorry for bad English)

(抱歉英语不好)

I have a bigproblem with performance of DataGridViewwhen it re-paints.

我在重新绘制时的性能有一个问题DataGridView

I'm using a DataGridViewto 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 DataGridViewimmediately when each new message comes, the DataGridViewdoesn't have time to re-paint itself before the next message comes.

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

A possible solution is to use a queue to collect messages and re-paint DataGridViewevery 100 ms with messages from queue. This is good but the DataGridViewblinks when it auto-scrolls to the last row. (Smooth scroll is disabled)

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

Can you help me to improve DataGridViewperformance?

你能帮我提高DataGridView性能吗?

回答by dandan78

I recently had some slowness issues with DataGridViewand the solution was the following code

我最近遇到了一些缓慢的问题,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);
}

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

它为DataGridView对象打开双缓冲。只需呼叫DoubleBuffered()您的 DGV。希望能帮助到你。

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.

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

回答by Jroc

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

看看 我的两个屏幕之一上 DataGridView 的可怕重绘性能

if you haven't already for some ideas

如果你还没有一些想法

回答by Tomas Kubes

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 .

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

回答by Mou

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

回答by Algernoncharles

When working with large amounts of data, the DataGridViewcontrol 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.

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

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

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