WPF Datagrid:延迟加载/无限滚动

发布于 2024-12-06 16:05:09 字数 394 浏览 1 评论 0原文

我用 250 行填充数据网格。当用户使用滚动条向下滚动时(例如低于 75%),我想从数据库中获取接下来的 250 行,依此类推。这个想法是,网格可能有数百万个结果,我们不想加载所有结果,直到用户请求它们。有现成的机制吗?

编辑:因为似乎有很多混乱:我不是在寻找标准数据虚拟化解决方案,我已经使用 它们。但它们都要求您提前指定“虚拟行”的数量,而该查询对我来说成本高昂。他们之所以需要它,是因为当您知道网格中的总项目时,它可以使计算当前页面/偏移量/等变得更加容易。但计算该金额是一个非常昂贵的 SQL 查询,因此我想迁移到另一个可以跳过 COUNT() 查询的解决方案。

I fill the Datagrid with 250 rows. When the user scrolls down using the scrollbar (below 75% for example), I want to fetch the next 250 rows from the database, and so on. The idea is that the grid could have millions of results and we don't want to load them all, until the user requests them. Is there an existing mechanism for this?

EDIT: Because there seem to be a lot of confusion: I'm not looking for the standard data virtualization solutions, I already use them. But they all require you to specify the number of 'virtual rows' in advance, and that query is to costly for me. The reason why they require it is because it's makes it so much easier to calculate the current page/offset/etc when you know the total items in the grid. But it is a very costly sql-query to calculate that amount, so I want to migrate to another solution where I can skip the COUNT() query.

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

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

发布评论

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

评论(2

何以笙箫默 2024-12-13 16:05:09

因此,看起来 DataGrid 的虚拟化属性对您没有帮助,因为它需要在 ItemsSource 中包含完整的数据集。

因此,要实现数据延迟加载(请参阅文章数据虚拟化),您可以处理 ScrollViewer.ScrollChanged 事件并应用经典的服务器端分页方法。基本上,您必须定义和计算诸如页面大小页码排序顺序之类的参数,这样您就可以从数据库请求所需的数据集并将其显示在 UI 上。每次当当前页码排序顺序发生变化时,您需要请求数据并更新网格的ItemsSource,也许您还需要也恢复滚动位置,但我对此不确定。

  • 计算可见项目的数量
  • 使用参数化查询向数据库发出数据请求,其中包含 PageNumber、PagiSize 等
  • 参数 通过刚刚加载的数据项目更新 DataGrid ItemsSource

我相信主要的挑战是计算页面大小、当前页码的值。我相信逻辑滚动模式会在这方面帮助您。

So looks like Virtualization property of DataGrid wouldn't help you because it requires a full data set to be in the ItemsSource.

So to have in place a data lazy loading (See an article Data Virtualization) You can handle ScrollViewer.ScrollChanged event and apply a classical server-side paging approach. Basically you have to define and calculate such sings like Page Size, Page Number, Sort Order in this way you can request from a data base a required data set and show it on UI. Each time when Current Page Number or Sort Order is changing you need to request a data and update ItemsSource of the grid, also perhaps you need to restore Scroll Position as well but I'm not sure in this.

  • Calculate number of visible items
  • Do a data request to database usign parametrized query with parameters like PageNumber, PagiSize
  • Update DataGrid ItemsSource by a just loaded data items

I believe a main challenge would be to calculate a value of Page Size, Current Page Number. I believe Logical Scrolling mode would help you in this.

南烟 2024-12-13 16:05:09

将 DataGrid 的 EnableRowVirtualization 属性设置为 true

因此,当用户滚动时将加载行,因此实际上只会加载可见行

Set DataGrid's EnableRowVirtualization property to true

Rows will therefore be loaded when user scrolls, so only the visible rows will actually be loaded

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