Skip to main content
A newer version of this page is available. .

Bind the WPF Data Grid to any Data Source with Virtual Sources

  • 3 minutes to read

Virtual Sources allow you to bind the GridControl to any data source, even if the total record count is unknown. The GridControl only requests top records or a certain page, and you can specify required data operations supported by a data source.

The image below shows the GridControl bound to a virtual source that fetches data from StackExchange:

VirtualSources

Bind to any Data Source

You can connect the GridControl to any data source: REST Services, NoSQL Databases, Custom WCF Services, EntityFramework or any other ORM.

VirtualSourcesBindingToAnyData

Virtual Source Types

Infinite Source

The GridControl bound to InfiniteAsyncSource displays data with infinite scrolling:

VirtualSourcesTutorialAll

View Example Run Demo

Paged Source

The GridControl bound to PagedAsyncSource displays data in pages:

VirtualSourcesPagedAsync

View Example Run Demo

Minimum Requirement

A data source needs to expose an API to fetch portions of data consecutively (for example, Skip and Take or fetch a certain data page by page index). Total item count is not required; sorting, filtering, and summary calculation operations are optional.

Enable Features

The GridControl bound to a virtual source only requests the top records or a certain page. You can specify which data operations are allowed, for example, an end user can sort by columns A, C, and D, and the Search Panel can filter data by the StartsWith criteria.

You can control requests that the GridControl sends to a service or database. This prevents overloading the database with non-optimal queries. The GridControl automatically hides all UI elements for non-supported operations.

Refer to the Virtual Sources Limitations topic to learn more.

Workflow

Async virtual sources raise events in the UI Thread and use tasks to process data in parallel Working Threads. You should provide tasks to these events to obtain summaries, rows, etc. Then the UI Thread gets data you fetched.

  1. (Optional step) The virtual source raises the InfiniteAsyncSource.GetTotalSummaries (or PagedAsyncSource.GetTotalSummaries) event. Handle this event and process summaries if you want to show them in the GridControl.
  2. The virtual source raises the InfiniteAsyncSource.FetchRows (or PagedAsyncSource.FetchPage) event to get the first portion of data.
  3. When end users navigate to the next page, the virtual source raises the InfiniteAsyncSource.FetchRows (or PagedAsyncSource.FetchPage) event to get the next portion of data.
  4. (Optional step) When users apply a filter, the virtual source raises the InfiniteAsyncSource.GetUniqueValues (or PagedAsyncSource.GetUniqueValues) event to get unique values and show them in a drop-down filter.

MVVM Support

You can implement data operations for virtual sources at the ViewModel level without UI dependencies.

View Example: MVVM in InfiniteAsyncSource View Example: MVVM in PagedAsyncSource

View Example: Bind the WPF Data Grid to Data View Example: Implement CRUD Operations

Follow the steps below to configure a virtual source and support the MVVM pattern:

  1. Assign an instance of the InfiniteAsyncSource or the PagedAsyncSource to the GridControl‘s ItemsSource property.

  2. Bind the instance’s properties to commands specified in the ViewModel.

Property Description
FetchRowsCommand Gets or sets a command that allows you to fetch rows.
GetUniqueValuesCommand Gets or sets a command that allows you to get unique values.
GetTotalSummariesCommand Gets or sets a command that allows you to get total summaries.
<dxg:GridControl>
    <dxg:GridControl.ItemsSource>
        <dx:InfiniteAsyncSource ElementType="{x:Type local:IssueData}" 
                                FetchRowsCommand="{Binding FetchIssuesCommand}"
                                GetUniqueValuesCommand="{Binding GetUniqueValuesCommand}"
                                GetTotalSummariesCommand="{Binding GetTotalSummariesCommand}"/>
    </dxg:GridControl.ItemsSource>
    <!-- ... -->
</dxg:GridControl>

Read Tutorial: How to use virtual sources in an MVVM application

See Also