November 30, 2010

How to increase speed of DataGridView or using DoubleBuffered property...

A few days ago I finished developing the output window of results. It's a window similar to Visual Studio Output Window. It has a few tabs and one tab was very-very annoying. Why? Because it first performance was very low... This tab is responsible for showing all messages from external compiler what we use to compile some source. For the displaying compiler messages we used standard DataGridView component. I don't like it but his layout is that what I need. When we start our application to have real tests we were sadden. Blinking, flicker, pulling... It was the terrible to think that someone will use this dung... Google is power! But not in this situation. Every query doesn't get appropriate results. Search and search.. Nothing... Only similar troubles with no smart answers. You say I search bad.. Maybe, but in that day I don't think that. In that day power has a stackoverflow. :) Yes, yes, yes... Two minutes and I have a great answer.
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);
}


Only one extension method to DataGridView and that's all. It's amazing..

Blinking, flicker, pulling.. No, no, no... Only smooth, clarity, speed... All what we need.

And only one question stay in my head. Why "DoubleBuffered" property is hidden? Maybe when whe set it to the true our application eats all RAM memory? Maybe our Duo Core Processors will work to the 100% to render data.. Hm.. Practical experience shown that with memory is all right (a little more, but a little) and with processor too.

MSDN says that this property indicating whether this control should redraw its surface using a secondary buffer to reduce or prevent flicker.