为数据绑定的数据网格视图实现虚拟模式 [英] implementing virtual mode for a datagridview that is databound

查看:35
本文介绍了为数据绑定的数据网格视图实现虚拟模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于实施建议的一般问题.

A general question for advice on the implementation.

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

I've got a collection bound to a datagridview.

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

我想实现 virtual mode 因为集合可能包含数百万个条目(Line 对象)所以我认为只缓存"或显示一个可能会更快一次需要的几个条目.哪个是我理解的虚拟模式?

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?

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

但我无法让它为 databounddatagridview 工作.

But I can't get it to work for a datagridview that is 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

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.'

推荐答案

如果你想使用 DataGridView.VirtualMode,那么你不应该使用绑定数据集.他们是对立的.因此,您无需设置 DataSource,而只需设置 RowCount 属性并为 DataGridView.CellValueNeeded 事件.

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.

另外你需要先将dataGridView.VirtualMode属性设置为true,大概是在设计器里写的.默认情况下它被设置为 false,这就是为什么你会得到一个异常,说你不能设置 RowCount.

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.

CellValueNeeded 事件将为每一行的每一列触发,具体取决于 RowCount 和列数.您应该根据 e.RowIndexe.ColumnIndex 使用事件处理程序中已处理单元格的值设置 e.Value>.

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.

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

So, for this to work you'll need at least to handle CellValueNeeded. If your DataGridView is readonly, others events are not necessary.

Windows 窗体 DataGridView 中的虚拟模式中提供了更完整和连续的概述控制.

这篇关于为数据绑定的数据网格视图实现虚拟模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆