WPF Datagrid:延迟加载/无限滚动
我用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,看起来 DataGrid 的虚拟化属性对您没有帮助,因为它需要在
ItemsSource
中包含完整的数据集。因此,要实现数据延迟加载(请参阅文章数据虚拟化),您可以处理 ScrollViewer.ScrollChanged 事件并应用经典的服务器端分页方法。基本上,您必须定义和计算诸如
页面大小
、页码
、排序顺序
之类的参数,这样您就可以从数据库请求所需的数据集并将其显示在 UI 上。每次当当前页码
或排序顺序
发生变化时,您需要请求数据并更新网格的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 whenCurrent Page Number
orSort Order
is changing you need to request a data and updateItemsSource
of the grid, also perhaps you need to restore Scroll Position as well but I'm not sure in this.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.
将 DataGrid 的
EnableRowVirtualization
属性设置为true
因此,当用户滚动时将加载行,因此实际上只会加载可见行
Set DataGrid's
EnableRowVirtualization
property totrue
Rows will therefore be loaded when user scrolls, so only the visible rows will actually be loaded