Skip to main content

Step 2: Enable Sorting

  • 2 minutes to read

In this step, you enable the sorting functionality in the GridControl bound to a virtual source.

Overview

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

  • Default - records are shown from new to old
  • Created Date in Descending order
  • Votes in any order

    public enum IssueSortOrder {
        Default,
        CreatedDescending,
        VotesAscending,
        VotesDescending
    }
    

    In Step 1: Fetch Data and Enable Scrolling, you have fetched rows with the default sort order:

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

To Enable Sorting

  1. Implement sorting in the virtual source:

    • Get the GridControl‘s sorting using the DevExpress.Data.FetchEventArgsBase.SortOrder property.
    • Parse sorting and return a sort order. The DevExpress.Data.InfiniteAsyncSource.FetchRows event handler takes into account this sort order when fetching rows.
        static IssueSortOrder GetIssueSortOrder(FetchRowsAsyncEventArgs e) {
            IssueSortOrder sortOrder = IssueSortOrder.Default;
            if (e.SortOrder.Length > 0) {
                var sort = e.SortOrder.Single();
                if (sort.PropertyName == "Created") {
                    if (sort.Direction != ListSortDirection.Descending)
                        throw new InvalidOperationException();
                    sortOrder = IssueSortOrder.CreatedDescending;
                }
                if (sort.PropertyName == "Votes") {
                    sortOrder = sort.Direction == ListSortDirection.Ascending
                        ? IssueSortOrder.VotesAscending
                        : IssueSortOrder.VotesDescending;
                }
            }
            return sortOrder;
        }
    
  2. Allow sorting in the GridControl by the Votes and Created column’s values. Set the ColumnBase.AllowSorting property to true for these columns.

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