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.
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 :)