Skip to main content
.NET 8.0+

Criteria Property of a List View's Collection Source

  • 2 minutes to read

The CollectionSourceBase.Criteria property of a List View’s ListView.CollectionSource allows you to filter the List View at data source level. This means that only objects that satisfy the specified criteria are retrieved from the database. This topic explains how to use this approach if you need to apply a filter which does not change at runtime or design time.

Do the following to access the Criteria property of a List View’s CollectionSource:

  1. Create a View Controller.
  2. Override the Controller’s OnActivated method to access the current ViewController.View object.
  3. Use the List View’s ListView.CollectionSource to retrieve the required objects from the data store.
  4. Add your filter to the CollectionSourceBase.Criteria filter collection.

The following code demonstrates how to show only objects whose FullName begins with an “A” in Person List Views:

using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.BaseImpl;
// ...
public class FilterPersonListViewController : ObjectViewController<ListView, Person> {
    protected override void OnActivated() {
        base.OnActivated();
        View.CollectionSource.Criteria["Filter1"] = CriteriaOperator.Parse("StartsWith([FullName], 'A')");
    }
}

This approach can be applied to any List View that you can define in code. In the code above, all List Views that display Person type objects are filtered.

A persistent object used in Collection Source’s Criteria does not reload when the Object Space is refreshed, which raises the SessionMixingException exception. To avoid this, use a persistent object’s key property instead of the object itself.

View.CollectionSource.Criteria["Filter1"] = CriteriaOperator.Parse("Manager.Oid = ?", ObjectSpace.GetKeyValue(manager));

Note

  • In nested List Views, the Criteria is not directly applied to the associated collection. Instead, a separate collection is created, and the criteria are applied to it; the original associated collection is not modified. A nested List View in the Client data access mode with a disabled proxy collection (see XafApplication.DefaultCollectionSourceMode) is an exception. In this instance, the criteria are directly applied to the associated collection.

  • Currently, TreeListEditor has limited filtering capabilities. Only root tree nodes are filtered if you specify the Collection Source’s Criteria property when filtering the List View.