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 (sample), 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. Please download this sample project - it contains a trial version of the XamGrid, so you can run the solution without any additional downloads. The project has been built using Visual Studio 2010, and targets Silverlight 4.
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
Here's a screenshot of the application:
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> 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
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 email@example.com