Skip to main content
A newer version of this page is available. .

Filtering UI Context

  • 5 minutes to read

DevExpress offers a number of different filtering options for its WinForms UI controls. Use Filtering UI Context to deliver user experiences that mirror web-based filter panels. This component creates a set of editors for users to filter data that a stand-alone, data-aware control displays. The Automatic Filtering UI demo illustrates a sample filtering UI generated for the Data Grid.

Filtering UI Context - Thumbnail

Supported Data-Aware Controls

The Filtering UI Context generates filter criteria and passes it to the data-aware control, which causes the latter to apply the corresponding filters. The Filtering UI Context can be used with all DevExpress data-aware controls that support filtering by criteria expressions.

Binding Modes

The Filtering UI Context generates a filtering UI from either a data-aware control directly or from a provided filtering Model.

  • Data-Aware Client (Simple Mode)

    The Filtering UI Context generates a filtering UI directly from a selected data-aware control. For this approach, you may have a reduced number of available customization options.

  • Filtering Model (Advanced Mode)

    A Model is a separate class that declares properties that match the fields of your data-aware control’s source.

    public partial class ProductFilteringModel {
        public string Category_Name { get; set; }
        public int Doors { get; set; }
        public decimal Price { get; set; }
        public bool? InStock { get; set; }
    }
    

    You can either create such a Model manually or use the same class a data-aware control utilizes when binding to code-first data sources like the Entity Framework.

Initially, the component’s smart tag is tuned to match the simple mode; click the “Enable Advanced Binding” link to browse advanced mode settings.

Filtering UI Context - Smarttags

Setting base and parent ViewModels are optional. See the Base and Parent Filtering ViewModels article to learn more.

Retrieve Fields and Create the Filter UI

When the component is bound to a client (e.g., a grid view) and to a filter control (e.g., an accordion control), you can retrieve data fields from the client and create the UI in the filter control. To do this, click Retrieve Fields in the component’s smart tag menu or call the RetrieveFields method in code.

filteringUIContext1.RetrieveFields();

After you call the RetrieveFields method in code, the following events fire.

  • FieldRetrieving - fires when a field is about to be retrieved. This event allows you not to create a layout item in the filter control for the processed field.
  • FieldRetrieved - fires after a field is retrieved. Allows you to customize the filter control’s layout item (AccordionControlElement or LayoutControlItem) created for the field.
using DevExpress.Utils.Filtering;
using DevExpress.XtraBars.Navigation;
//…
filteringUIContext1.FieldRetrieving += filteringUIContext1_FieldRetrieving;
filteringUIContext1.FieldRetrieved += filteringUIContext1_FieldRetrieved;
filteringUIContext1.RetrieveFields();
//…
private void filteringUIContext1_FieldRetrieving(object sender, FilteringUIFieldRetrievingEventArgs e) {
    if (e.PropertyName == "ID")
        e.Cancel = true;
}
private void filteringUIContext1_FieldRetrieved(object sender, FilteringUIFieldRetrievedEventArgs e) {
    AccordionControlElement element = e.Item as AccordionControlElement;
    if (e.PropertyName == "Name")
        element.Text = "NAME";
}

If you use the component in the advanced mode (you provide your own filter model), you can add fields by using the AddField method, and then call the RetrieveFields method to create the filter control’s layout items.

filteringUIContext1.AddField("Products.Stock", typeof(Boolean), BooleanUIEditorType.Toggle, "In Stock");
filteringUIContext1.RetrieveFields();

Refresh Bindings

If data in the Filtering UI Context’s source changes, you can call the ResetBinding/ResetBindings methods to manually refresh editor values.

//refresh all data fields' values
filteringUIContext1.ResetBindings();
//refresh the specific data field
filteringUIContext1.ResetBinding("InStock");

Filtering UI

The Filtering UI Context populates either an Accordion Control or a Layout Control with items. Utilize the component’s smart-tag to choose an appropriate container (or create one if none are available).

Filtering UI Context - Container

A generated Accordion\Layout item contains an editor (or multiple editors) that fits the filtered data type. For instance, to filter numeric field data, the component generates two text boxes and a RangeTrackBarControl. You can remove these editors, customize them, or switch to other editor types by clicking the link at design time (see the figure below).

Filtering UI Context - Change Type

The Filtering UI Context generates editors only once when you click the “Retrieve Fields” link in a component’s smart tag or call the “RetrieveFields” method in code. If a data source changes, you need to re-generate a filtering UI.

Handling specific events and adding filtering attributes also changes a filtering UI.

See Also