Software Development
Blogs and Discussion
developer.*
Books Articles Blogs Subscribe d.* Gear About Home

Quick Fixes for SLOW DataGridView

After learning that you really don't want to try using the DataGridView in unbound mode (just get your data into a DataTable and let binding do the heavy lifting--binding doesn't suck anymore!), I had to next learn that the DataGridView can be really, really slow after you get more than a trivial amount of data in it.

For example, in a winforms app I'm working on, with a couple thousand rows in it, the grid would pause for up two seconds after the user clicked a checkbox in a cell. Ouch. The grid was also very slow to load, even using some tricks with SuspendLayout() and ResumeLayout().

Turns out all this slowness is due to painting issues, not data issues. Before you give up on the DataGridView as hopelessly slow, try these things:

* Set both AutoSizeColumnsMode and AutoSizeRowsMode to "None." If you want to call the AutoSizeColumns() method one time after you load the grid, that works pretty well, especially if you auto size based on the headers. You can also resize certain columns using the AutoSizeColumn() method or by setting a literal value for the Width propery.

* Set EnableHeadersVisualStyles to False. You will lose a little bit of "slickness" in your grid's appearance, but it will speed up considerably. It seems that enabling this feature makes the grid "inherit" its visual style from the OS style. Check the documentation or intellisense for a more specific description.

* I saw a suggestion on this page to turn off cell borders (CellBorderStyle = DataGridViewCellBorderStyle.None), but I did not try this one myself.

That same page linked above also confirms that the .NET 2.0 DataGridView is slower than the .NET 1.1 DataGrid. They must not have done a lot of testing with the autosizing and visual styles turned on. Hopefully a future release will improve things, but I shall not be holding my breath since I half suspect Microsoft is going to start letting winforms languish in favor of WPF...

This page also points to a DataGridView FAQ document that has some useful information.

Hope that helps,
Dan

Categories: 

Another tip to speed up DataGridView

In unbound (non-Virtual) mode it's much faster to add all the rows at the beginning and then fill the cells than it is to add the rows one at a time inside the filling loop.

e.g.

Rather than:

MaxRows = 10000
For i = 0 to MaxRows - 1
dgv.Rows.Add()
dgv.Rows(i).Cells(0) = "Some Data"
dgv.Rows(i).Cells(1) = "Some More Data"
Next i

Use:

MaxRows = 10000
dgv.Rows.Add(MaxRows)
For i = 0 to MaxRows - 1
dgv.Row(i).Cells(0) = "Some Data"
dgv.Row(i).Cells(1) = "Some More Data"
Next i

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

User login

About our advertising.

Atom Feed

developer.* Blogs also has an Atom feed, located at this url.

Click here for more information about Atom.

A Jolt Award Finalist
Software Creativity 2.0
Foreword by Tom DeMarco

Recent Posters

Based on most recent 60 days, sorted by # of posts and name.

Google
Web developer.*

Who's online

There are currently 0 users and 25 guests online.

Syndicate

Syndicate content
All views expressed by authors, bloggers, and commentors are their own and do not necessarily reflect the views of developer.* or its proprietors.
Click to read the Copyright Notice.

All content copyright ©2000-2005 by the individual specified authors (and where not specified, copyright by Read Media, LLC). Reprint or redistribute only with written permission from the author and/or developer.*.

www.developerdotstar.com