20

(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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Yuriy
  • 2,670
  • 6
  • 33
  • 48
  • possible duplicate of [Horrible redraw performance of the DataGridView on one of my two screens](http://stackoverflow.com/questions/118528/horrible-redraw-performance-of-the-datagridview-on-one-of-my-two-screens) – Tomas Kubes Jan 08 '15 at 09:26

6 Answers6

47

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.

dandan78
  • 13,328
  • 13
  • 64
  • 78
  • 9
    The method you posted uses reflection to change a non-public property on a `DataGridView` control. This is an acceptable solution, but it's probably cleaner just to inherit off of the existing `DataGridView` control and create your own custom control with the "DoubleBuffered" style set. – Cody Gray - on strike Nov 23 '10 at 11:07
  • My problem is that if I derive my own control then the form in the editor won't display. – Andrew Truckle Jun 09 '16 at 20:30
9

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

Community
  • 1
  • 1
Jroc
  • 488
  • 2
  • 11
7

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 .

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • 1
    And if you expand MyForm.cs in the Solution Explorer, you can open MyForm.Designer.cs and make it instantiate the double buffered subclass instead of the base class. – Timo Apr 22 '15 at 13:50
  • 3
    Thanks for this - it's night and day different. To anyone wondering how to do this - add this class to the bottom of your code - I added it within my namespace outside of my form class. Then, open your form's `Designer.CS` (`Form1.Designer.cs`) and click the `+` sign next to `Windows Form Designer generated code` to unhide the section. Change the datagridview initialization (something like `this.dataGridView1 = new System.Windows.Forms.DataGridView`) - change it to `this.dataGridView1 = new DataGridViewDoubleBuffered`. Then, scroll down to the bottom where it has its declarations. – user1274820 Dec 27 '17 at 22:40
  • 2
    Change the declarations (`private System.Windows.Forms.DataGridView dataGridView1`) to `private DataGridViewDoubleBuffered dataGridView1`. I also went ahead and made my form double buffered - might be worth doing - not sure if it's necessary. – user1274820 Dec 27 '17 at 22:41
3

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

VMAtm
  • 27,943
  • 17
  • 79
  • 125
3

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 });
Mou
  • 15,673
  • 43
  • 156
  • 275
3

Also read MSDN article: Best Practices for Scaling the Windows Forms DataGridView Control

Alex Burtsev
  • 12,418
  • 8
  • 60
  • 87