问题
My code looks as follows:
private void dataGridView4_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
Records recordsTmp = null;
recordsTmp = (Records)this.ArrayOfRecords[e.RowIndex]; //ArrayList with string[] objects inside
switch (dataGridView4.Columns[e.ColumnIndex].HeaderText)
{
case "#":
e.Value = recordsTmp.nr;
break;
case "ip":
e.Value = recordsTmp.Ip;
break;
case "long":
e.Value = recordsTmp.Long;
break;
case "3":
e.Value = recordsTmp.type;
break;
case "4":
e.Value = recordsTmp.time;
break;
}
ArrayOfRecords
is updated with 10-100 newstring[]
objects per second.VirtualMode
is set to true.SelectionMode
is set toFullRowSelect
.dataGridView
is Read-only.
Now theres also a ProgressBar
with Marquee
style which shows me that with lets say 5000+ rows scrolling freezes the Form
but i guess its just a matter of threading/backgroundworker etc.
What scares me most is selection. Having 8000 rows and clicking last one (8000) takes my form 4.2 seconds to select it. And its as follows:
4000 rows makes it 2.1 secs, etc. If 8000th row is selected minimazing and then maximizing takes 4.2 secs. Selecting row 1 "makes it" happy again. Its unacceptable. *Why is it so hard to "mark" 8000th row for my Form
?
VirtualMode's pagination works perfectly but selection is a pain.
Theres also another issue/behaviour:
Why CellValueNeeded
event is raised when im moving my mouse over the rows? They are already painted? So why is it wasting resources?
Is there any solution for this selection problem? Or i have to limit maximum records in the datagrid
回答1:
One thing you want to play with is the autosizing of the cells, as the gridview will have to go through all your cells in order to find the one with the longest length. You should disable autosizing and you should do that programatically. Regarding your selection problem: selecting causes a lot of redraws, my hunch is that is redraws all your cells, therefore the lag is proportional to your row/cell count.
In order to use Virtual Mode you need more than setting the VirtualMode to true. It would be interesting to see your dataGridView1_RowsAdded, dataGridView1_CellValuePushed and dataGridView1_CellValidating methods, since they are probably the culprit of the delays.
来源:https://stackoverflow.com/questions/8260167/datagridview-virtual-mode-and-lags