WPF Datagrid:延迟加载/无限滚动 [英] WPF Datagrid: Lazy Loading / Inifinite scroll

查看:2573
本文介绍了WPF Datagrid:延迟加载/无限滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



编辑:因为似乎有很多困惑:我不是寻找标准的数据虚拟化解决方案,我已经使用他们。但是他们都需要提前指定虚拟行的数量,这个查询对我来说代价是昂贵的。他们需要它的原因是因为它使得它更容易计算当前页/偏移量/ etc,当你知道网格中的总项目。但是它是一个非常昂贵的sql查询来计算这个金额,所以我想迁移到另一个解决方案,我可以跳过COUNT()查询。

解决方案

看起来像DataGrid的Virtualization属性不会帮助你,因为它需要一个完整的数据集在 ItemsSource



所以要有一个数据延迟加载(参见文章数据虚拟化)您可以处理 ScrollViewer.ScrollChanged 事件并应用经典的服务器端分页方法。基本上你必须定义和计算如页面大小页码排序顺序这样,您可以从数据库请求所需的数据集,并在UI上显示它。每次当前页码排序顺序正在更改您需要请求数据并更新 ItemsSource 的网格,也许你需要还原滚动位置,但我不知道在这个。




  • 计算可见项目数

  • 使用诸如PageNumber,PagiSize等参数对数据库执行参数化查询的数据请求

  • 更新DataGrid ItemsSource一个刚刚加载的数据项



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


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.

解决方案

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.

这篇关于WPF Datagrid:延迟加载/无限滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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