Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datagrid: Lazy Loading / Inifinite scroll

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.

like image 294
Maestro Avatar asked Sep 28 '11 10:09

Maestro


People also ask

Is lazy loading the same as infinite scroll?

Infinite scroll uses lazy loading and executes its demand to load more data (products or content) at the bottom of the page, without requiring an action such as the click of a button. On-demand loading is used to optimize content delivery by reducing time consumption and memory usage.

What is the difference between pagination and lazy loading?

Pagination offers limited engagement to the user so that they can find a particular item immediately. On the other hand, lazy loading provides more interaction to the user, and they tend to spend more time on their first visit.

What is the infinite scroll?

Infinite Scroll is a JavaScript plugin that automatically adds the next page, saving users from a full page load. You've likely seen it in use all over the web.

What is load more?

“Load more” allows the user to compare more easily products across an entire list. Having one consolidated list of goods made it significantly easier for users to evaluate which products would be the best to navigate to and, consequently, increased the overall product discoverability rate.


1 Answers

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.

like image 149
sll Avatar answered Sep 27 '22 19:09

sll