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

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

问题描述

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

因为似乎有很多困惑:我不是在寻找标准的数据虚拟化解决方案,我已经在使用 他们.但是它们都要求您提前指定虚拟行"的数量,而该查询对我来说成本很高.他们之所以需要它,是因为当您知道网格中的总项目数时,它可以更轻松地计算当前页面/偏移量/等.但是计算该数量是一个非常昂贵的 sql 查询,所以我想迁移到另一个解决方案,在那里我可以跳过 COUNT() 查询.

解决方案

所以看起来 DataGrid 的 Virtualization 属性对您没有帮助,因为它需要 ItemsSource 中的完整数据集.

因此要进行数据延迟加载(请参阅文章数据虚拟化) 您可以处理 ScrollViewer.ScrollChanged事件并应用经典的服务器端分页方法.基本上,您必须定义和计算诸如 Page SizePage NumberSort Order 之类的歌曲,这样您就可以从数据库请求所需的数据集并将其显示在 UI 上.每次 Current Page NumberSort Order 发生变化时,您都需要请求数据并更新网格的 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 数据网格:延迟加载/无限滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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