Datagridview virtual mode, RowCount update causes CellValueNeeded to run all rows

I am trying to implement datagridview virtual mode, but when I set RowCount to some number (to show the scroll bar), the grid wants to have all the rows at once, not just the ones displayed.

DataGridView grid = new ...; grid.VirtualMode = true; grid.CellValueNeeded += OnCellValueNeeded; grid.RowCount = dataprovider.GetFullCount(); 

How can I tell the grid to request only the displayed rows?

+4
source share
5 answers

Unfortunately, this is similar to standard behavior. I could solve it either

 void OnCellValueNeeded(...) { if(!_active) return; } grid.VirtualMode = true; grid.CellValueNeeded += OnCellValueNeeded; _active = false; grid.RowCount = dataprovider.GetFullCount(); _active = true; 

or an implementation of IBindingList, ITypedList with complex lazy fetching in the background thread

Update: The problem is now fixed. I can no longer play it using the following:

 static class Program { private static Form form; private static int i; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var grid = new DataGridView { Dock = DockStyle.Fill, VirtualMode = true, AllowUserToAddRows = false, Columns = { new DataGridViewTextBoxColumn { HeaderText = "foo" }, new DataGridViewTextBoxColumn { HeaderText = "bar" }, }, }; grid.CellValueNeeded += OnCellValueNeeded; form = new Form { Controls = { grid } }; //grid.RowCount = 0; grid.RowCount = 10000; Application.Run(form); } private static void OnCellValueNeeded(object sender, DataGridViewCellValueEventArgs e) { i++; form.Text = i.ToString(); e.Value = "fooValue"; } } 
0
source

This is just an assumption, but are there any AutoSizeRowsMode or AutoSizeColumnsMode values ​​set for AllCells, or any of the columns set for this? Try setting the resize mode to None or Only Displayed and see if there is a problem.

+4
source

Not sure if this is the same problem as mine, but I had very poor performance with regular radical changes to the RowCount in VirtualMode DataGridView.

I noticed that the scroll bar was changing "slowly"; that is, it looked like it was deleting individual virtual lines (!).

In any case, doing grid.Rows.Clear() before each call to grid.RowCount = n dramatically improves performance.

+2
source

I have the same problem, and I tried the solution to set the active flag like you, and I also tried the solution to set the RowCount to 0 (or grid.Rows.Clear ()) before setting up a new RowCount.

Both of these things have improved performance, but have not accelerated it as much as I want, since I dynamically filter the grid in real time based on input from the search box.

I found two other solutions:

1) Use pagination so that you do not need to set the RowCount so high as to start with. I think this is a great solution if you are already using pagination (in which case you will not be here), but too cumbersome if you do not plan to implement it.

2) Place a call to install the RowCount in your own thread. This is the one I'm going to do. Honestly, I’m not sure how safe it is if you try to edit cells while the stream is still ending, but I think I will find out soon.

EDIT:

OK, so I tried streaming, hoping it would be a trick since I read elsewhere that it really helped the other guy. This seems to be a good solution if you are going to change the value once in a while, but it still hangs if you do it several times in a row (which I am). I think this is due to the fact that you should use Invoke (), and the second time you are still waiting for the completion of the first. I can’t say that I fully understand what a deal is, but I decided to just live with empty rows, because this SOOO is much faster and less complicated when I just leave them there.

0
source

You must set the RowCount value to zero before setting a full account.

 DataGridView grid = new ...; grid.VirtualMode = true; grid.CellValueNeeded += OnCellValueNeeded; grid.RowCount = 0; grid.RowCount = dataprovider.GetFullCount(); 
0
source

Source: https://habr.com/ru/post/1384969/


All Articles