Link to home
Start Free TrialLog in
Avatar of tomataus
tomataus

asked on

How do I improve DataGridView Performance?

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.

SCENARIO:

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.

MY OBSERVATIONS
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.

MY ATTEMPTS TO SOLVE IT
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 :)
Avatar of RobbP
RobbP
Flag of South Africa image

Hi,

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.
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson05.aspx
Avatar of tomataus
tomataus

ASKER

Hi,

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
 
  AttachAllDataGridEventHandlers();
  carDataGridView.Refresh();
  // 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.
ASKER CERTIFIED SOLUTION
Avatar of SwissKnife
SwissKnife
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.