implementing virtual mode for a datagridview that is databound

不羁岁月 提交于 2019-12-01 08:10:10

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!