Version

Filtering XamDataGrid

This topic will describe the filtering behaviors that can be implemented with the XamDataGrid control.

Required Background

The following topics are prerequisites to understanding this topic:

Topic Purpose

This topic provides basic steps required for adding the XamDataGrid control to your view and populating it with sample data.

This resource topic provides implementation of sample data that you can use a data source for the XamDataGrid control.

Overview of Filtering behavior

The XamDataGrid control allows you to apply record filtering to reduce the displayed records to a user based on a certain criteria. Record filtering of the XamDataGrid control utilizes the FilterFactory class to construct custom filter expressions, which may be used singly or in multiples by chaining expressions together.

Filtering can be implemented on Local and Remote data sources; for more information, refer to the following topics:

The following steps demonstrate how to implement filtering:

  1. Create an instance of the FilterFactory class.

  2. Assign the PropertyPath of the Column to be filtered to the FilterFactory instance, by passing the key in to the Property property, exposed from the FilterFactory object.

  3. Assign a filter condition to the FilterFactory instance, by accessing one of the condition extension methods exposed from the Property property, passing in the desired value to filter by.

  4. Add the entire FilterFactory expression to FilterExpressionCollection collection of the XamDataGrid control.

Filtering Requirements

The XamDataGrid control accepts filter expressions which are constructed via the FilterFactory class; whose basic expression can be broken down into two fundamental components including: a target field to be filtered; this is passed into the Property as the PropertyPath of the filtered Column and a filter operator method, which is associated with a conditional value.

The FilterFactory class supports a wide variety of filter conditions, including the basic logical operations of and, or, not, contains, greaterThan, etc, (please reference the FilterFactory API for a full list of these operations) which are applied via their extension methods; these methods are appended to the filter expression immediately following the Property API member. These operator extension methods accept a single parameter, which consists of the value to be filtered by.

Filtering Records of Single Column

The following example demonstrates how to implement a basic filtering expression with the XamDataGrid control. In this scenario, records of the Territory Column will be filtered, so that only those records including the country of “Bulgaria” are displayed.

This example uses the SampleGridApp from the Getting Started with XamDataGrid topic.

  1. Create a reference to the FilterFactory interface as demonstrated in the following code snippet.

In C#:

var Filter = Infragistics.Core.Controls.DataSource.FilterFactory.Instance;
  1. Assign the target field and a filter expression to the FilterFactory instance and then pass the entire expression into the Add() method of FilterExpressions.

The target field is set by accessing the Property property of the FilterFactory instance and is passed in as the PropertyPath of the Column that you want to filter.

The filter expression methods are accessed from the Property() method and the value to be filtered-by is passed in as its parameter.

In this case, you are using the isEqualTo expression method so that the filtered content only contains those records with the Territory of “Bulgaria”.

In C#:

DataGrid.FilterExpressions.Add(Filter.Property("Territory").IsEqualTo("Bulgaria"));
  1. Save and run the SampleGridApp to verify the result.

The XamDataGrid control will filter its records, so that only those records with the Territory of Bulgaria are displayed, as demonstrated in the following screenshot.

Filtering with DataGridView 1.png

Filtering Records of Multiple Columns

The XamDataGrid control supports the implementation of multiple filter expressions and conditions applied to the FilterFactory instance; this is possible by chaining expressions together, thus allowing for complex filter conditions that filter multiple Columns and values, simultaneously.

The use of multiple filter expressions is demonstrated in the following example. In this scenario, all employees whose assigned Territory includes “Bulgaria” or “Japan” and whose sales are within the range and including the values of ‘10000’ and ‘20000’ will be displayed in the filtered records.

  1. Modify the data source of the XamDataGrid control such that it has at least 1000 data items. This will ensure that you have a sufficient number of results from your data set once the complex filter condition is applied.

In C#:

var DataGrid = new XamDataGrid();
DataGrid.ItemsSource = SampleSalesPerson.GenerateSalesData(1000);
  1. Create a reference to the FilterFactory interface as demonstrated in the following code snippet.

In C#:

var Filter = Infragistics.Core.Controls.DataSource.FilterFactory.Instance;
  1. Access the XamDataGrid control’s FilterExpressions property and use its corresponding Add method to pass in your chained filter expression as its parameter.

Within the Add method, pass in the FilterFactory instance and use its Property method to target the Territory Column, by passing the “Territory” key in as its parameter, then access the isEqualTo extension method and pass in the string value of “Bulgaria”.

Append an OR condition to the first expression, by accessing the Or method from the previous expression and as its parameter, pass in the FilterFactory instance, this time targeting the Territory Column; access the isEqualTo extension method and pass in the string value of “Japan”.

Append the Add method to the previous expression and pass in the FilterFactory instance, access its Property method and target the Sales Column, by passing the “Sales” key as the Property methods parameter; access the IsGreaterThanOrEqualTo extension method and pass in the value of XamDataGrid as its parameter.

Lastly, append an Add method to the previous expression, passing in the FilterFactory instance, access it Property method and target the Sales Column once more, by passing the “Sales” key in as the parameter value and then access the IsLessThanOrEqualTo extension method and pass in the value of '20000' for its parameter.

In C#:

DataGrid.FilterExpressions.Add(Filter.Property("Territory").IsEqualTo("Bulgaria")
    .Or(Filter.Property("Territory").IsEqualTo("Japan"))
    .And(Filter.Property("Sales").IsGreaterThanOrEqualTo(10000))
    .And(Filter.Property("Sales").IsLessThanOrEqualTo(20000)));
  1. Save and run the SampleGridApp to verify the result. The XamDataGrid control will filter its records so that only those which include Japan and Bulgaria in their Territory Column and whose Sales values are within the range of 10000 and 20000 will be displayed within its UI as illustrated in the following screenshot.

Filtering with DataGridView 2.png

Related Content

The following table lists topics that are related to this topic:

Topic Purpose

This topic provides information on supported column types in the XamDataGrid control.

This topic provides code examples on working with columns in the XamDataGrid control.

This topic provides information and code examples on sorting behaviors in the XamDataGrid control.