I'm trying to implement datagridview's virtual mode but when i set RowCount to some number (to show the scrollbar) the grid wants to have all rows at once, not only the displayed.
DataGridView grid = new ...;
grid.VirtualMode = true;
grid.CellValueNeeded += OnCellValueNeeded;
grid.RowCount = dataprovider.GetFullCount();
How can i tell the grid to only request the rows displayed?
You should set the RowCount to zero before setting the full count.
I'm having the same issue and I tried the solution of setting an active flag like you did, and I also tried the solution of setting the RowCount to 0 (or grid.Rows.Clear()) before setting the new RowCount.
Both of these things improved performance, but neither sped it enough to be as instantaneous as I want because I am dynamically filtering the grid in real time based on input from a search box.
I found two other solutions:
1) Use pagination so that you don't have to set the RowCount so high to begin with. I think this is a great solution if you are already using pagination (in which case you wouldn't be here) but an overly cumbersome one if you weren't planning on implementing it.
2) Put the call to set the RowCount in its own thread. This is the one I'm about to attempt. Honestly not sure how safe this one is if you're trying to edit cells while the thread is still finishing up but I guess I'll find out soon enough.
EDIT:
OK, so I tried threading it hoping that would do the trick since I read elsewhere that it really helped another guy. It seems to be a good solution IF you are just going to change the value once in a while but it still hangs if you do it multiple times consecutively (which I am). I think this is because you have to use Invoke() and the second time around it's still waiting for the first one to finish. Can't say I totally understand what the deal is but I've decided just to live with the blank rows for now because it's SOOO much faster and less complex when I just leave them there.
unfortunatly this seems to be the standard behavior. i could solve it either by
or implementing IBindingList, ITypedList with complex lazy fetching in background thread
Update: the problem seems to be fixed now. I can not reproduce it anymore using the following:
This is just a guess, but do you have the AutoSizeRowsMode or AutoSizeColumnsMode values set to AllCells or are any of the columns set to that either? Try setting the resize mode to None or just DisplayedCells and see if there is still a problem.
Not sure if this is the same problem as I was having, but I did get very poor performance when regularly drastically changing the RowCount on a VirtualMode DataGridView.
What I noticed was that the scroll bar was changing "slowly"; i.e. it looked like it was individually removing my virtual rows (!).
Anyway, doing a
grid.Rows.Clear()
before each call togrid.RowCount = n
drastically improved performance.