Using Multi-Column Filtering in the XamGrid

Kiril Matev / Monday, July 12, 2010

Let’s say you’re well along the path implementing your application, when your users turn to you with a request to filter records in one of the XamGrids using a multi-column filtering condition. You’re familiar with XamGrid single-column filtering, and are wondering whether you’ll have to change existing logic at the data source level for multiple-column conditions.

Actually, there’s a way you can do this directly using the XamGrid API – this is what this blog post is all about.

Let’s say you're building an inventory-tracking system, and for each item, keep data on how many units there are in stock (UnitsInStock), how many units are on order (UnitsOnOrder), together with a reorder level (ReorderLevel). Our users classify an inventory item as critical, when the sum of the quantities currently in stock and on order is less than the reorder level. Essentially, the filtering rule condition is:

UnitsInStock + UnitsOnOrder < ReorderLevel

In implementing this behavior, we create an instance of the RowsFilter class to refer to the bound item type, and the column the custom filtering logic will be applied to. We then create an instance of the CustomComparisonCondition class, and set its Expression member to a LINQ expression implementing the custom logic:

CustomComparisonCondition c = new CustomComparisonCondition();
//create a boolean clause implementing the condition
System.Linq.Expressions.Expression<Func<Product, bool>> expr = product => product != null &&
product.UnitsInStock + product.UnitsOnOrder < product.ReorderLevel;
c.Expression = expr;

//create a filter for one of the column
RowsFilter rf = new RowsFilter(typeof(Product), this.igGrid.Columns.DataColumns["UnitsInStock"]);
//add the custom condition to the filter for the column
rf.Conditions.Add(c);

This RowFilter then has to be added to the RowFiltersCollection of the XamGrid to have the condition rule applied.

Implementing a filter condition in this way does not prevent your users from using the filtering capabilities of the XamGrid. Furthermore, they will not be able to see, change or remove the filtering condition you have added as described above. Any filtering operations they perform will be combined with the custom filtering condition you have added, and will only produce subsets of the set of records produced by your custom filtering condition alone.

This functionality can save you time and resources by giving you a straight-forward way to implement multi-column filtering conditions in the XamGrid. You won’t have to change existing code, use built-in filtering functionality, and produce readable and easily maintainable code.

If you have any questions, do not hesitate to email me at kmatev@infragistics.com