【问题标题】:implementing virtual mode for a datagridview that is databound为数据绑定的 datagridview 实现虚拟模式
【发布时间】:2012-10-14 12:48:13
【问题描述】:

关于实施的一般性问题。

我有一个绑定到 datagridview 的集合。

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

我想实现virtual mode,因为该集合可能包含数百万个条目(Line 对象),所以我认为一次只“缓存”或显示一些需要的条目可能会更快。我理解虚拟模式的用途是什么?

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

但我无法让它为datagridview 工作,即databound

我无法指定行数:

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

编辑:此链接表明我可能必须完全删除绑定。是这样吗? http://msdn.microsoft.com/en-us/library/ms171622.aspx

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

【问题讨论】:

    标签: c# .net data-binding datagridview virtualmode


    【解决方案1】:

    如果你想使用DataGridView.VirtualMode,那么你不应该使用绑定数据集。他们是对立的。因此,您无需设置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 事件将为每一行的每一列触发,具体取决于 RowCount 和列数。您应该根据e.RowIndexe.ColumnIndex 在事件处理程序中使用已处理单元格的值设置e.Value

    因此,要使其正常工作,您至少需要处理 CellValueNeeded。如果您的 DataGridView 是只读的,则不需要其他事件。

    Virtual Mode in the Windows Forms DataGridView Control 提供更完整和连续的概述。

    【讨论】:

    • 嗯,好的,谢谢您的回复。我以为它们是相互补充的。但异常是不言自明的,虚拟模式已设置为 true。
    猜你喜欢
    • 2011-08-10
    • 2011-01-02
    • 2011-06-11
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多