Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

.net - How to improve painting performance of 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...