C# 在虚拟模式下从数据网格检索行时出现异常

发布于 2024-07-29 08:42:31 字数 439 浏览 5 评论 0原文

当我从虚拟模式数据网格检索行列表时,我不断收到异常(见下文),只有当我的行数超过屏幕上可以显示的行数时才会发生这种情况,并且不会每次都发生。 关于虚拟模式我还缺少什么吗?

更新> 下图显示了问题,索引现在超出了列表范围。 这样做的原因是说我有 10 个项目,我隐藏了 5 个,因为不需要它们,我想在可见的 5 个项目上运行一些代码,现在有 5 个项目,但有些项目的索引可能在 5-9 之间,如何我可以重新索引吗? 当我在可见的 5 上运行一些代码时,我会显示隐藏的 5,所以我不想丢弃它们,当它们全部可见时,我需要再次重新索引。 非常感谢。

替代文本

I keep getting an exception (see below) when I retrieve a list of rows from a Virtual Mode datagrid, this only happens when I have more rows than I can display on screen and it doesn't happen every time. Is there anything I'm missing with regards to virtual mode?

Update> The image below shows the problem, the index is now outside the list range. The reason for this is say I have 10 items and I hide 5 as they are not needed and I want to run some code on the 5 that are visible, there are now 5 items but the index of some maybe between 5-9, how can I re-index? When I have run some code on the visible 5 I then show the hidden 5 so I don't want to disgard these, I'd need to reindex again when they are all visible. Many thanks.

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光沙漏 2024-08-05 08:42:31

我只使用了 DataGridView 的虚拟模式,但在虚拟模式下,您应该自己存储数据,并在数据网格需要渲染时提供它。 我认为数据网格仅创建填充视图所需的最小数量的项目,然后出于性能原因重用它们。

I've only used virtual mode with DataGridView, but in virtual mode you are supposed to store the data yourself and provide it when the datagrid needs to for rendering. I suppose the datagrid is only creating the minimum number of items that is needed to fill the view and then reuse them for performance reasons.

半衾梦 2024-08-05 08:42:31

对我来说,这看起来有点像线程问题。 您是否使用BackgroundWorker或类似的东西来填充_items变量? 看起来 _items 中的事物数量在循环时正在发生变化。

也许您可以尝试用以下方法包围处理 _items 的任何和所有代码:

lock (_items) 
{
    // your code
}

或者,如果您的填充/更改代码仍在运行,则突破这些函数并返回 null(也许使用某种布尔哨兵变量)。

This looks sort of like a threading problem to me. Are you using a BackgroundWorker or something similar to populate the _items variable? It looks like the number of things in _items is changing while you are looping over it.

Perhaps you could try surround any and all code dealing with _items with this:

lock (_items) 
{
    // your code
}

Alternatively, break out of these functions and return null if your population/alteration code is still running (use some kind of boolean sentinel variable perhaps).

烟沫凡尘 2024-08-05 08:42:31

您如何设置数据绑定。

您是否可以针对底层数据集而不是通过数据网格进行工作。

How have you set up your databinding.

Would it be possible for you to work against the underlying dataset instead of going via the datagrid.

猫烠⑼条掵仅有一顆心 2024-08-05 08:42:31

请注意 DataGridView 的虚拟模式限制。 尽管它具有所谓的低内存消耗功能(仅从数据库加载可见行); 每行都有自己的实例(为了证明,尝试调整每个 DataGridView 行的大小,行的大小可以彼此独立设置,因此每行的信息(例如 RowHeight)需要保存在自己的内存中)

当您设置 RowCount ,它将根据您在 RowCount 中指定的内容实例化n 行。 从而违背了DataGridView虚拟模式的最初目的之一,低内存消耗。 内存消耗过多可能会减慢数据网格的显示速度,

请参阅我的文章 http://www. codeproject.com/KB/grid/DataGridView_Billion_Rows.aspx,以及另一篇文章 http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/243a81e7-909b-4c8e-9d28-6114248cf66e

Just be aware of DataGridView's virtual mode limitations. Despite of its supposed low-memory consumption feature (only visible rows gets loaded from database); each row has their own instance (to prove, try to resize each of DataGridView rows, rows' sizes can be set independently of each other, thus each row's information(e.g. RowHeight) need be saved in their own memory)

When you set the RowCount, it will instantiate n number of rows from what you specified in RowCount. Thus defeating one of the original purposes of DataGridView's virtual mode, low-memory consumption. Too much memory consumption could slow down your datagrid display

See my article here http://www.codeproject.com/KB/grid/DataGridView_Billion_Rows.aspx, and the other article http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/243a81e7-909b-4c8e-9d28-6114248cf66e

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文