Skip to main content

Step 1: Fetch Data

  • 2 minutes to read

This step describes how to create a virtual source and fetch rows from a service.

WinUI Grid - Virtual Sources Fetch

View Example: Data Grid - Bind to InfiniteAsyncSource

Fetch Data

  1. Add the GridControl with columns that correspond to the Issues Service’s IssueData fields to your project:

    <dxg:GridControl x:Name="grid" AutoGenerateColumns="False">
        <dxg:GridControl.Columns>
            <dxg:GridTextColumn FieldName="Subject" Width="150"/>
            <dxg:GridTextColumn FieldName="User" Width="50"/>
            <dxg:GridDateColumn FieldName="Created" Width="50" MaskType="DateTime" Mask="g"/>
            <dxg:GridTextColumn FieldName="Votes" Width="50"/>
            <dxg:GridTextColumn FieldName="Priority" Width="50"/>
        </dxg:GridControl.Columns>
    </dxg:GridControl>
    
  2. Initialize a virtual source:

    public sealed partial class MainPage : Page {
        public MainPage() {
            this.InitializeComponent();
    
            var source = new InfiniteAsyncSource() {
                ElementType = typeof(IssueData)
            };
        }
    }
    
  3. Dispose of the virtual source. Handle the Page’s Unloaded event and call the VirtualSourceBase.Dispose method:

    public MainPage() {
        // ...
        Unloaded += (o, e) => {
            source.Dispose();
        };
    }
    

    If you do not dispose of a source, the thread in which the source responds to requests can slow down. You may not dispose of a source if the source’s lifetime coincides with the application’s lifetime.

  4. Fetch rows from the data source:

    public MainPage() {
        // ... 
        source.FetchRows += (o, e) => {
            e.Result = FetchRowsAsync(e);
        };
    }
    static async Task<FetchRowsResult> FetchRowsAsync(FetchRowsAsyncEventArgs e) {
        var take = 30; // The number of rows that the GridControl loads in one portion.
        var issues = await IssuesService.GetIssuesAsync(
            skip: e.Skip,
            take: take,
            sortOrder: IssueSortOrder.Default,
            filter: null);
        return new FetchRowsResult(issues, hasMoreRows: issues.Length == take);
    }
    

    Tip

    Refer to the following topic for information on how to allow users to sort data: Step 2: Enable Sort Operations.

    Refer to the following topic for information on how to allow users to filter data: Step 3: Enable Filter Operations.

  5. Assign the virtual source’s instance to the DataControlBase.ItemsSource property:

    public MainPage() {
        // ...
        grid.ItemsSource = source;
    }
    

Continue or Review