Skip to main content
All docs
V22.1
.NET Framework 4.5.2+

Filter List Views

  • 3 minutes to read

This lesson describes three techniques used to filter a List View. Filters will be applied to the Contact List View.

Note

Before you proceed, take a moment to review the following lessons:

Activate a Filter Action

This technique allows end-users to apply predefined filters to a List View.

  1. In the MySolution.Module project, open the Model.DesignedDiffs.xafml file in the Model Editor. Navigate to the Views | MySolution.Module.BusinessObjects | Contact_ListView node. Right-click the Filters child node and select New | ListViewFilterItem. For the new node, set the Id property to Development Department Contacts. Set the Criteria property to [Department.Title] = 'Development Department'.

    Development Department ListViewFilterItem properties

    It is necessary to use the Criteria Language Syntax to specify Criteria properties. See the following help topic for more information: Criteria Language Syntax. You can also invoke the Filter Builder dialog to simplify the process used to create the filter criteria. Click the ellipsis button (EllipsisButton) to the right of the Criteria value. Within this dialog, you can visually design a criteria expression.

    Filter Builder

  2. Add another child node to the Filters node. Set the Id property to Developers and the Criteria property - to [Position.Title] = 'Developer'.

    Developer ListViewFilterItem properties

  3. Add one more child node to the Filters node. Set the Id property to All Contacts and leave the Criteria property empty. This item will display all Contact objects in the List View.

    All Contacts ListViewFilterItem properties

  4. For the Filters node, set the CurrentFilter property to Developers. The Developers filter will be initially applied to the Contact List View.

    Filters node properties

  5. Run the application and check that the SetFilter Action is available.

    Filters node properties

Use the Model Editor to filter a List View

You can apply filters to a List View in the Model Editor. End-users cannot change these filters.

  1. In the MySolution.Module project, open the Model.DesignedDiffs.xafml file in the Model Editor. Navigate to the Views | MySolution.Module.BusinessObjects | Contact_ListView node. Set its Criteria property to Position.Title = 'Developer'.

    Apply filters in the Model Editor

  2. Run the application and check that the Contact List View displays only developers.

    Filtered List View

Filter on Data Source Level

You can create filters that cannot be changed at runtime or in the Model Editor.

  1. In the Solution Explorer, right-click the Controllers folder in the MySolution.Module project, and choose Add DevExpress Item | New Item… to invoke the Template Gallery. Select the XAF Controllers | View Controller Visual Studio template, specify FilterListViewController as the new item’s name and click Add Item. As a result, you will get an autogenerated FilterListViewController.cs file with a single View Controller declaration.

  2. Derive the controller from the ObjectViewController<ViewType, ObjectType> as shown below:

    using DevExpress.Data.Filtering;
    using DevExpress.ExpressApp;
    using MySolution.Module.BusinessObjects;
    // ...
    
    public partial class FilterListViewController : ObjectViewController<DetailView, Contact> {
        public FilterListViewController() {
            InitializeComponent();
        }
        // ...
    }
    
  3. Override the OnActivated method. Use the CollectionSourceBase.Criteria property to specify a filter criteria:

    public partial class FilterListViewController : ObjectViewController<DetailView, Contact> {
        // ...       
        protected override void OnActivated() {
            base.OnActivated();
            View.CollectionSource.Criteria["Developers"] = CriteriaOperator.FromLambda<Contact>(c => c.Position.Title == "Developer");
        }
        // ...
    }
    
  4. Run the application and check that the Contact List View is filtered.

Next Lesson

Group List View Data