Performances on DataGridView Control

Today I was trying to increase performances on a Windows Form application (a Smart Client application) and I've observed that one of the controls that needs a big attention is absolutely the DataGridView (I use it to load data from the remote database).

Although the DataGridView control is absolutely one of the most useful controls, absolutely a must when you've to display data on a formatted grid, I think that this control is not totally optimized and its simplicity in terms of usage sometimes could be the source of problems.

One of the big lacks in terms of performances for example is obtained when you set the automatic rows and columns resizing. As explained on a little chapter on MSDN, Best Practices for Scaling the Windows Forms DataGridView Control, to determine the correct size, the DataGridView control must examine the value of each cell that it must accommodate and this is terrible if you've a grid with lots of data.

The best practice says that, in order to avoid performance penalties, you've to use the following guidelines:

  • Avoid using automatic sizing on a DataGridView control with a large set of rows. If you do use automatic sizing, only resize based on the displayed rows. Use only the displayed rows in virtual mode as well.

  • For maximum scalability, turn off automatic sizing and use programmatic resizing.

This is a common operation however. Often developers sets the AutoSizeColumnsMode and AutoSizeRowsMode property of the DataGridView to AllCells to have an automatic resize.

This works good on a testing environment and that's all. However, when you deploy your application on a production environment and you're application is under an heavy load, performances of the grid will decrease a lot.

To have better performances, you can set the Autosize properties to None and use programmatic resizing, but not always a developer wants this (although it could be the best choice for big applications I think).

To have good performances also under heavy load and to avoid programmatic resizing, the best choice are as follow:

For rows and columns, use the DisplayedCells or DisplayedCellsExceptHeaders field of the DataGridViewAutoSizeRowsMode, DataGridViewAutoSizeColumnsMode, and DataGridViewAutoSizeColumnMode enumerations.

For row headers, use the AutoSizeToDisplayedHeaders or AutoSizeToFirstHeader field of the DataGridViewRowHeadersWidthSizeMode enumeration.

With these settings the grid works good and you can avoid lots of pain (it can take lots of seconds to display data on a grid with lots of data and autosize enabled).

These are the things to remember...

Print | posted on Wednesday, April 19, 2006 3:04 PM

Comments on this post

No comments posted yet.

Your comment:

 (will show your gravatar)
 
Please add 8 and 5 and type the answer here: