实现了一个数据绑定

问题描述:

一个一般性的问题有关实施意见一个DataGridView虚拟模式。实现了一个数据绑定

我有绑定到一个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

'如果绑定模式不符合您的性能需求,您可以通过虚拟模式事件处理程序管理自定义缓存中的所有数据。

如果你想使用DataGridView.VirtualMode,那么你就不能指望用绑定数据集。他们atr相反。因此,您不需要设置DataSource,而只需设置RowCount属性并为DataGridView.CellValueNeeded Event提供事件处理程序。

另外你需要先设置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.RowIndexe.ColumnIndex设置e.Value与事件处理程序中处理的单元格的值。

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

更完整和连续的概述,请Virtual Mode in the Windows Forms DataGridView Control

+0

好的,谢谢你的回复。我认为他们互相补充。但例外情况是自我解释,虚拟模式已被设置为true。 – Jimmy