Skip to main content

Step 2: Enable Sort Operations

  • 2 minutes to read

This step describes how to allow users to sort rows:

  • Implement sort operations in the virtual source.
  • Enable these operations in the GridControl.

WinUI Grid - Virtual Sources Sort

View Example: Data Grid - Bind to InfiniteAsyncSource

Sort Orders

The Issues Service allows you to apply the following sort orders:

  • Default - records are shown from newest to oldest
  • Created Date in Descending order
  • Votes in any order
public enum IssueSortOrder {
    Default,
    CreatedDescending,
    VotesAscending,
    VotesDescending
}

The Step 1: Fetch Data topic demonstrates how to fetch rows with the default sort order:

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

Allow Users to Sort Rows

  1. Implement sort operations in the virtual source:

    static IssueSortOrder GetIssueSortOrder(SortDefinition[] sortOrder) {
        if (sortOrder.Length > 0) {
            var sort = sortOrder.Single();
            if (sort.PropertyName == "Created") {
                if (sort.Direction != ListSortDirection.Descending)
                    throw new InvalidOperationException();
                return IssueSortOrder.CreatedDescending;
            }
            if (sort.PropertyName == "Votes") {
                return sort.Direction == ListSortDirection.Ascending
                    ? IssueSortOrder.VotesAscending
                    : IssueSortOrder.VotesDescending;
            }
        }
        return IssueSortOrder.Default;
    }
    
    static async Task<FetchRowsResult> FetchRowsAsync(FetchRowsAsyncEventArgs e) {
        IssueSortOrder sortOrder = GetIssueSortOrder(e.SortOrder);
        var take = 30;
        var issues = await IssuesService.GetIssuesAsync(
            skip: e.Skip,
            take: take,
            sortOrder: sortOrder,
            filter: null);
        return new FetchRowsResult(issues, hasMoreRows: issues.Length == take);
    }
    
  2. Allow users to sort GridControl rows by the Votes and Created columns. Set the ColumnBase.AllowSorting property to true for these columns:

    <dxg:GridDateColumn FieldName="Created" AllowSorting="True"/>
    <dxg:GridTextColumn FieldName="Votes" AllowSorting="True"/>
    

Continue or Review