实现虚拟模式一个DataGridView即数据绑定(implementing virtual mod

2019-07-02 12:37发布

有关实施意见的一般问题。

我有绑定到一个集合datagridview

BindingList<Line> allLines = new BindingList<Line>();
dataGridView1.DataSource = allLines;

我想要实现virtual mode因为集合可能包含数百万条目( Line对象),所以我想它可能是更快地只是“缓存”或显示需要在同一时间几个条目。 这就是我理解虚拟模式是什么?

我看: http://msdn.microsoft.com/en-us/library/2b177d6d.aspx

但我不能让它的工作datagridviewdatabound

我不能指定的行数:

this.dataGridView1.RowCount = 20;
`RowCount property cannot be set on a data-bound DataGridView control.`

编辑:这个链接提示我可以有完全去除结合。 这样的话? http://msdn.microsoft.com/en-us/library/ms171622.aspx

“如果结合模式不能满足您的性能需求,你可以通过虚拟模式的事件处理程序自定义缓存管理所有的数据。”

Answer 1:

如果你想使用DataGridView.VirtualMode ,那么你就不能指望用绑定数据集。 他们ATR对立面。 所以,你不设置DataSource ,但只设置RowCount属性,并提供事件处理程序DataGridView.CellValueNeeded事件 。

除此之外,您还需要设置dataGridView.VirtualMode属性为true第一,大概写在设计师。 默认情况下,它被设置为false ,这就是为什么你会得到一个异常,说你不能设置RowCount

也许你将不得不手动初始化网格列。

虽然刷新了您的网格(比如按钮点击),你必须

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.

CellValueNeeded活动将针对每行每列取决于行数和列数被解雇。 你预计将设置e.Value与处理的细胞在事件处理取决于价值e.RowIndexe.ColumnIndex

所以,对于这个工作,你需要至少处理CellValueNeeded 。 如果你的DataGridView是只读的,其他事件是没有必要的。

更完整和连续的概述,请在Windows虚拟模式窗体DataGridView控件 。



文章来源: implementing virtual mode for a datagridview that is databound