A general question for advice on the implementation.
I've got a collection bound to a datagridview
.
BindingList<Line> allLines = new BindingList<Line>();
dataGridView1.DataSource = allLines;
I want to implement virtual mode
because the collection could contain millions of entries (Line
objects) so I thought it may be quicker to only 'cache' or display a few entries that are needed at a time. Which is what I understand virtual mode to be for?
I've looked at: http://msdn.microsoft.com/en-us/library/2b177d6d.aspx
But I can't get it to work for a datagridview
that is databound
.
I can't specify the row count:
this.dataGridView1.RowCount = 20;
`RowCount property cannot be set on a data-bound DataGridView control.`
EDIT: this link suggests I may have to remove the binding completely. Is this the case? http://msdn.microsoft.com/en-us/library/ms171622.aspx
'If bound mode does not meet your performance needs, you can manage all your data in a custom cache through virtual-mode event handlers.'
If you want to use DataGridView.VirtualMode, then you're not expected to use bound data set. They atr opposites. So, you don't set DataSource
, but just set the RowCount
property and provide an event handler for DataGridView.CellValueNeeded Event.
Besides you need to set dataGridView.VirtualMode
property to true
first, probably write in the designer. By default it's set to false
, that's why you get an exception, saying you cannot set RowCount
.
Probably you'll have to manually initialize grid's columns.
While refreshing your grid (say, on button click), you'll have to
dataGridView.RowCount = 0;
\\refresh your cache, where you store rows for the grid
\\...
dataGridView.RowCount = yourCache.Count;//or some other property, getting the number of cached rows.
The CellValueNeeded
event will be fired for each column of each row, depending on the RowCount and the number of columns. You're expected to set e.Value
with the value of the processed cell in your event handler depending on the e.RowIndex
and e.ColumnIndex
.
So, for this to work you'll need at least to handle CellValueNeeded
. If your DataGridView is readonly, others events are not necessary.
A more complete and consecutive overview is available at Virtual Mode in the Windows Forms DataGridView Control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With