Skip to main content
All docs
V23.2

Change List View Filters

  • 3 minutes to read

This lesson describes three techniques you can use to filter the objects in a List View.

In your application, the Employee List View shows all employees at once. The instructions below show how to apply one of the following filters to this List View:

  • Display only employees from the Development department
  • Display only developers
  • Display all employees

Add Multiple Predefined Filters

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 | Employee | Employee_ListView node. Right-click the Filters child node and select Add | ListViewFilterItem from the context menu.

    When you add items to the Filters node, the built-in SetFilter Action becomes available in the UI.

  2. Specify the following properties for the new node:

    • Set the Id property to Development Department Employees.
    • Set the Criteria property to [Department.Title] = 'Development Department'.

    Development Department ListViewFilterItem properties

    Criteria properties use Criteria Language Syntax.

    You can also construct a filter criteria in the Filter Builder dialog. To open this dialog, click the ellipsis button to the right of the Criteria value.

    Filter Builder

  3. Add another child node to the Filters node and specify the following properties:

    • Set the Id property to Developers.
    • Set the Criteria property to [Position.Title] = 'Developer'.

    Developer ListViewFilterItem properties

  4. Add one more child node to the Filters node and set the Id property to All Employees. Leave the Criteria property empty. This item will display all Employee objects in the List View.

    All Contacts ListViewFilterItem properties

  5. For the Filters node, set the CurrentFilter property to Developers. This way the Employee List View initially displays only those objects whose Position property is set to Developer.

    Filters node properties

  6. Run the application and check that the SetFilter Action is available:

    ASP.NET Core Blazor
    Filters Action in ASP.NET Core Blazor
    Windows Forms
    Filters Action in Windows Forms

Set a Static Filter in the Model Editor

The Model Editor allows you to apply a static filter to List View records. This way, the List View initially displays only those records that fit a specified criteria and the application’s UI doesn’t have a filter Action.

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

    Apply filters in the Model Editor

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

    ASP.NET Core Blazor
    ASP.NET Core Blazor Filtered List View
    Windows Forms
    Windows Forms Filtered List View

Set a Filter in Code

You can create filters that cannot be disabled in the application UI nor 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.

  2. Select the XAF Controllers | View Controller Visual Studio template. Specify FilterListViewController as the new item’s name and click Add Item.

  3. In the autogenerated FilterListViewController.cs file, inherit the controller from the ObjectViewController<ViewType, ObjectType>:

    using DevExpress.Data.Filtering;
    using DevExpress.ExpressApp;
    using MySolution.Module.BusinessObjects;
    // ...
    
    public partial class FilterListViewController : ObjectViewController<ListView, Employee> {
        public FilterListViewController() {
            InitializeComponent();
        }
        // ...
    }
    
  4. Override the OnActivated method:

    public partial class FilterListViewController : ObjectViewController<ListView, Employee> {
        // ...       
        protected override void OnActivated() {
            base.OnActivated();
            //Specify a filter criteria.
            View.CollectionSource.Criteria["Developers"] = CriteriaOperator.FromLambda<Employee>(c => c.Position.Title == "Developer");
        }
        // ...
    }
    
  5. Run the application and check the filter in the Employee List View.

Next Lesson

Group List View Data