Criteria Property of a List View's Collection Source

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.

Note the following limitations when implementing this approach in Mobile applications:

  • Filtering aggregated collections is not supported;
  • Mobile applications do not support the Function Criteria Operators listed in the Function Criteria Operators topic's "Limitations of Function Criteria Operators used in Mobile Applications" section;
  • This approach do not support Custom Function Criteria Operators;
  • Using object property values as criteria operands is not supported.

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.

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.