We help IT Professionals succeed at work.

How do I improve DataGridView Performance?

tomataus asked
My problem lies in the DataGridView.RowCount. If RowCount was originally 1000, then I change it to 300, I experience major sluggishness. But please read my scenario below first to know when it happens exactly.


I have a DataGridView that displays data in Virtual mode. I used the Virtual mode example from MSDN (http://msdn2.microsoft.com/en-us/library/2b177d6d.aspx). I use the same DataGridView to display a list of cars filtered by their color.

e.g. Filtering by the color Red returns 500 cars;
Blue returns 500 cars;
Yellow returns 500 cars;
All Colors returns 1500 cars
* when I say "returns 500 cars", that's only the virtual row count, not the actual number of rows returned.

The filtering is done thru my SQL statement (which is already as optimized and as fast as possible).

All is working great and response time was negligible.  That is until I tried getting All Colors, scrolled all the way down to the bottom of the DataGridView, and then switched to view only a particular color of a car. Suddenly, my response time look quite a bit longer.

NOTE, I experience this if the number of rows started at a large number before switching to fewer rows. Scrolling down increases the wait time even more.

This slowdown occurs the moment I change the value of DataGridView.RowCount (e.g. 1000) to a smaller value (e.g 300). I noticed that the excess 700 rows all become unshared (I watch the RowUnshared event to check when this happens) one by one in reverse order, that is, row number 1000 becomes unshared, then 999, etc, as if the DataGridView was removing each row one by one. I dunno if that's what the DataGridView really does or that unshared rows has anything to do with my particular problem but I did read in MSDN (http://msdn2.microsoft.com/en-us/library/ha5xt0d9.aspx) that unshared rows can affect performance.

I've tried only one thing so far... I detach all the event handlers for my DataGridView (e.g. CellValueNeeded, SelectionChanged, etc.) before changing the data (before setting RowCount), and reattach them again once my data is all set (after setting RowCount). Only a slight improvement.

Hope you can help me.... Thanks :)
Watch Question


Are you using datasets as the source ie disconnected, seems to me your getting lag because your connecting to the db every time you query, if you use a disconnected datasate as your source for the datagridview - you can also filter the dataset.



Thanks for the comment.

I am using disconnected datasets, but my datagrid does not bind to the dataset itself. The data is only shown on the datagrid when the event CellValueNeeded is called. In fact, I have no problems with the speed when connecting to the database. In fact, it is faster when my code retrieves data from the db every time I need a new page or when changing filters compared to setting the RowCount to a smaller value.

My code looks something like this:

// Refer to the article from msdn for the cache 
// class in my first post under "SCENARIO"
// I've also modified the cache so it has the RowCount 
// property which is the total records found that meet 
// the filter criteria. It is not the actual row's retrieved.
// I've also modified the DataPageRetrieved to be able
// to get more than just 2 pages.
private Cache _cache;
// Event I need to handle in order to display 
// data in Virtual mode
private void carDataGridView_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
  // Get the value from the cache that will be 
  // displayed in the cell
  e.Value = _cache.RetrieveElement(e.RowIndex, e.ColumnIndex);
// Function called to get a new list of cars based 
// on color or all colors
public void GetCarList(string color)
  DetachAllDataGridEventHandlers(); // just to help improve performance
  // Get new cache of car list with 50 records per page. 
  // There are 3 pages worth of data retrieved.
  _cache = CreateCarListCache(color, 50); // EXECUTES FAST
  // This next line of code executes slowly if orginal 
  // value of RowCount was way bigger than the new RowCount,
  // e.g. from 1000 to 300
  carDataGridView.RowCount = _cache.RowCount; // EXECUTES SLOWLY
  // Remember, CellValueNeeded is never called at this
  // point because it's detached
  // All data is now refreshed to reflect new data
  // because CellValueNeeded is now attached

Open in new window


Ooops. My comment in line 8 of the code should be DataPageRetriever, not DataPageRetrieved.
Hello tomataus

The property carDataGridView.RowCount only sets the number of rows to display. If you change from 1000 to 300 rows, 700 rows are deleted one by one, and 300 old rows are kept.
If you add another 300 rows, the kept roes are deleted also.
I guess you should clear the rows collection with carDataGridView.Rows.Clear() to get rid of all rows at once.

Here is a link to the RowCount property:


Oh my god! Why didn't I think of that? It's perfect.


As it shows, sometimes the solution is so obscure, yet the best solution is really the simplest one.
This one works great.