DataGridView is slowwwwww....

BugMan

Well-known member
Joined
Dec 27, 2010
Messages
52
Location
Tallahassee FL
Programming Experience
10+
I realize this has been covered here & there, but I'm wondering what the best solution is. My dgv has only 20 columns and 20 rows, some with colored background, and is painfully slow even on a relatively fast laptop.

Some solutions I've seen include setting the dgv property to Doublebuffered, but I don't understand how to do this, it's not in the Properties box. Another suggestion mentioned add range instead of adding a row at a time.

What's the best solution? Thanks in advance.
 
On second thought... I don't think adding a range instead of a row at a time will matter. The grid is already drawn on the screen, and if I minimize and then return to normal, it takes a couple of seconds to draw the screen. Very disappointing, hope there is a solution.
 
The only time any of my DGVs have slowed down to that speed is when there are conversion errors being caught by Try...Catch...End Try when trying to set cell colours. Have you checked that?
 
I've not seen slowness in the DataGridView control that has bothered me, but I have some comments still. Are you using CellFormatting event? If so note this:
To avoid performance penalties when handling this event, access the cell through the parameters of the event handler rather than accessing the cell directly.
It is noticable to set the cell style directly, ie Item().Style, from this event rather than using recommended e.CellStyle.
Other code may also affect the processing, for example comment for same event:
The CellFormatting event occurs every time each cell is painted, so you should avoid lengthy processing when handling this event.
If you are using CellFormatting event and don't need to dynamically set the style then set it beforehand and don't use CellFormatting event for this.

Also, are you using Windows XP? This could explain that you see more painting occur than me on Vista, which has a different and a lot better paint engine. Basically all windows (controls) are doublebuffered in Vista.

About the protected DoubleBuffered property, I can also see this has some effect on initial painting, but don't know what possible other side effects it may have to set it. Anyway, here's the VB translation, try it yourself:
VB.NET:
Dim p = GetType(DataGridView).GetProperty("DoubleBuffered", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
p.SetValue(Me.DataGridView1, True, Nothing)
Alternative is to inherit the DataGridView class where you do have access to this property:
VB.NET:
Public Class DBDataGridView : Inherits DataGridView
    Sub New()
        Me.DoubleBuffered = True
    End Sub
End Class
 
JohnH,

I'm not using the CellFormatting event at all. I am running Windows 7 64bit.

Does the first code just go into the Form1 Class with all of my other code? I put it there & get an error on the 2nd line of that: "Declaration expected"

If I were to try the 2nd code snip, would I add a new Class form to the project and put it in there? Do I substitute my name for the dgv for your "DBDataGridView"?

Thanks for your patience... I'm quite the hack, aren't I? :)
 
Last edited:
I'm not using the CellFormatting event at all. I am running Windows 7 64bit.
That should be fine, relevant to know.
Does the first code just go into the Form1 Class with all of my other code? I put it there & get an error on the 2nd line of that: "Declaration expected"
Yes, Form Load event handler would be a good place.
If I were to try the 2nd code snip, would I add a new Class form to the project and put it in there?
Yes.
Do I substitute my name for the dgv for your "DBDataGridView"?
You can name a class anything you want, as long as it follows the naming conventions ;)
 
VB.NET:
Dim p = GetType(DataGridView).GetProperty("DoubleBuffered", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
p.SetValue(Me.DataGridView1, True, Nothing)

Yeee hiiiiii!!!! That does it! Put it into my form1 load event 1st thing. The difference is night & day.

Another problem solved by JohnH, can't thank you enough.
 
Back
Top