Sunday 2 September 2012

Double Buffering Windows DataGridView

Years ago I wrote a WinForms application, that I still use every day. I tinker with the source code pretty much every day too. One of my recent issues to address was the rendering speed of the DataGridView controls; they'd take, roughly, forever to display around 30 items.

I did what I could think of to speed up the rendering (mainly around optimising data access) but couldn't get the rendering time down. Then I Googled it, and it appears that I'm not the first developer to have this problem; the DataGridView is notoriously slow to render. The annoying part is that the problem is so easy to fix - set the grid's DoubleBuffered property on.

This property is protected, so you need to either use Reflection to set it on your grid instance, or create a specialised DataViewGrid. I created a DoubleBufferedDataGridView:

When I use this in place of the DataGridView, the results are pretty good. A test app shows the time taken (ms) to render a sample datasource of 16 items on a standard DataGridView, versus a DoubleBufferedDataGridView.

It's so easy to fix, it seems odd that DoubleBuffered is not set to true by default, but hey.
The code for the sample form above is available as a GitGist

No comments:

Post a Comment