Skip to content

DevExpress-Examples/xaf-how-to-sort-a-listview-in-code

Repository files navigation

XAF - How to sort a ListView in code

This example sorts list view data by a class property and prevents users from modifying the sorting settings.

Data sorted by Modified On column value

Implementation Details

  1. Create a view controller in the application model and configure sorting settings for the list view's column:

    File to review: SortListViewController.cs

    protected override void OnActivated() {
        base.OnActivated();
        string propertyName = nameof(Issue.ModifiedOn);
        bool demoFlag = true;
        // This code applies a client side sorting.
        if(demoFlag) {
            IModelColumn columnInfo = View.Model.Columns[propertyName];
            if(columnInfo != null) {
                columnInfo.SortIndex = 0;
                columnInfo.SortOrder = ColumnSortOrder.Descending;
            }
        } else {
            // This code is used for the server side sorting.
            if(View.Model.Sorting[propertyName] == null) {
                IModelSortProperty sortProperty = View.Model.Sorting.AddNode<IModelSortProperty>(propertyName);
                sortProperty.Direction = SortingDirection.Descending;
                sortProperty.PropertyName = propertyName;
            }
        }
    }
  2. Implement platform-dependent controllers that disable the sorting functionality in underlying grid controls:

    File to review: BlazorSortListViewController.cs

    protected override void OnViewControlsCreated() {
        base.OnViewControlsCreated();
        if(View.Editor is DxGridListEditor gridListEditor) {
            foreach(DxGridDataColumnModel columnModel in gridListEditor.GridDataColumnModels) {
                columnModel.AllowSort = false;
            }
        }
    }

    File to review: WinSortListViewController.cs

    protected override void OnViewControlsCreated() {
        base.OnViewControlsCreated();
        if(View.Editor is GridListEditor gridListEditor) {
            gridListEditor.GridView.OptionsCustomization.AllowSort = false;
        }
    }

This approach allows you to sort both nested and root list views, and works if server mode is enabled in the list view.

Documentation

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Sort list view in code and prevent users from modifying the sorting settings

Topics

Resources

License

Stars

Watchers

Forks

Contributors 6