Using Data Virtualization
As the case of handling a lot of data is quite common, the requirement for fast data processing becomes more and more indispensable. The result of this necessity is the Data Virtualization technique that ensures much better performance as well as user experience improvements.
The virtual collection is designed for ReadOnly purposes only and it is not recommended to be used in other scenarios when updates are required. Add/insert scenario is not supported with VirtualQueryableCollectionView as it cannot maintain cache of the already loaded items.
When working with the UI components that enable UI virtualization, you may take advantage of the above mentioned technique by using VirtualQueryableCollectionView class. It enables you to benefit from the on-demand data loading to smooth scrolling with UI virtual components. VirtualQueryableCollectionView provides you with the following important members:
-
LoadSize: Gets or sets a value that defines the maximum number of items requested at once.
-
VirtualItemCount: Gets or sets a value that defines the total number of items available on the server-side.
-
ShouldEnumeratorLoadItems: Gets or sets a value that indicates whether items that are not loaded yet should get loaded while the collection's enumerator is traversed.
-
ItemsLoading: An event that will be raised when the collection is requesting item at some index and this item is not already loaded. The arguments in this event are as follows:
- StartIndex: Requested item index.
- ItemCount: Number of requested items (can be less than or equal to the LoadSize).
When using VirtualQueryableCollectionView for WPF, you may easily benefit from the built-in sorting, grouping, filtering, etc. functionality if you provide IQueryable as a source and just set LoadSize property to desired value. For example:
Example 1: Wrap a query in a VirtualQueryableCollectionView
public MainWindow()
{
InitializeComponent();
var context = new NorthwindEntities();
var query = context.Order_Details.OrderBy(o => o.OrderID);
var view = new VirtualQueryableCollectionView(query) { LoadSize = 10 };
DataContext = view;
}In the example above Entity Framework is used. However, you may use Linq to SQL, OpenAccess or any other Linq provider in the same manner.
When ScrollMode is configured as Deferred, a ScrollPositionIndicator is shown as the user scrolls vertically. Its content is empty until the user releases the scrollbar and the items are loaded. To hide the indicator permanently, define a Style for it that sets its Opacity to 0.
Bind RadGridView to the virtual collection set as the DataContext in Example 1.
Example 2: Binding RadGridView
<telerik:RadGridView ItemsSource="{Binding}" />
The controls that currently support data virtualization are RadGridView, RadComboBox, RadTreeView, RadCarousel, and RadBook.
Filtering is only supported with IQueryable data sources.
Filtering on Distinct Values is not fully supported when using VirtualQueryableCollectionView. We suggest configuring GridViewDataColumn with ShowDistinctFilters="False". That way only filtering through the Field Filters will be enabled. See basic filtering for more information about filtering support and Field Filters.
ItemsLoading event
ItemsLoading event will be raised if you try to access some item by index and this item is not yet loaded. When you scroll down, the ItemsLoading event will be called and the empty(null) items in the collection will be replaced with the new items. When an item is already loaded the collection will not call ItemsLoading event for this item.
When you provide IQueryable in the VirtualQueryableCollectionView you do not need to handle the ItemsLoading event.
When ScrollMode is configured as Deferred, a ScrollPositionIndicator is shown as the user scrolls vertically. Its content is empty until the user releases the scrollbar and the items are loaded. To hide the indicator permanently, define a Style for it that sets its Opacity to 0.