Manual Data Source Operations
The ListView lets you fetch the current page of data on demand through the OnRead event. This can let you optimize database queries and return only a small number of records.
In this article you will find examples how to:
- implement custom paging
- implement filtering and sorting
Custom Paging
This is, effectively, loading data on demand only when the user goes to a certain page, as opposed to the default case where you fetch all the data items initially.
To implement your own paging in the listview, you need to:
- Handle the
OnReadevent. - Set the current page of data to the
args.Dataproperty of the event argument. - Set the
args.Totalproperty to the total number of items on all pages, so that the pager displays correct information. - Set the
TItemattribute of the ListView to the model type. - Do not set the component
Dataattribute.
Custom Paging in the ListView
Filter and Sort
While the listview does not have built-in UI for filtering and sorting like a grid does, you can add your own components to invoke such actions and simply update the data source of the component.
The example below shows a relatively simple way to filter and sort over all data in the current view model without loading data on demand.
Filter and Sort data in a listview
To optimize queries, you can store the
DataSourceRequestfrom theOnReadevent in a view-model field to easily access the current page.You can also use the Telerik extension methods - the
.ToDataSourceResult()that takes aDataSourceRequestargument over the full collection of data and add filer and sort descriptors to it. Examples of doing that are available in the Live Demos: ListView Filtering and ListView Sorting