实现虚拟模式一个DataGridView是数据绑定 [英] implementing virtual mode for a datagridview that is databound

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

问题描述

有一个普遍的问题有关实施意见。

A general question for advice on the implementation.

我有绑定到一个 datagridview的集合

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

我想实施虚拟模式因为集合可能包含数以百万计的条目(对象),所以我想它可能会更快,只'缓存或显示所需要的时间那几个条目。这就是我理解的虚拟模式是?

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 /库/ 2b177d6d.aspx

但我不能让它开始工作了 datagridview的数据绑定

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

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

'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 ,那么你就不能指望用绑定数据集。他们ATR对立。所以,你不要设置数据源,而只是将行数属性,并提供一个事件处理程序<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalueneeded.aspx"相对=nofollow> 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 属性第一,可能是写在设计师。默认情况下它被设置为,这就是为什么你得到一个异常,说你不能设置行数

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 事件将被发射的每一行的每一列,根据行数和列数。你预计将设置 e.Value 与处理细胞的视 e.RowIndex e.ColumnIndex

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控制

A more complete and consecutive overview is available at Virtual Mode in the Windows Forms DataGridView Control.

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

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