[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How do I improve DataGridView Performance?

Posted on 2008-01-24
6
Medium Priority
?
5,122 Views
Last Modified: 2013-12-17
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 :)
0
Comment
Question by:tomataus
  • 4
6 Comments
 
LVL 3

Expert Comment

by:RobbP
ID: 20740874
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
0
 

Author Comment

by:tomataus
ID: 20741010
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

0
 

Author Comment

by:tomataus
ID: 20741268
Ooops. My comment in line 8 of the code should be DataPageRetriever, not DataPageRetrieved.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 3

Accepted Solution

by:
SwissKnife earned 2000 total points
ID: 20762418
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:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.rowcount.aspx
0
 

Author Comment

by:tomataus
ID: 20765469
Oh my god! Why didn't I think of that? It's perfect.
0
 

Author Closing Comment

by:tomataus
ID: 31424892
As it shows, sometimes the solution is so obscure, yet the best solution is really the simplest one.
This one works great.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question