Generic Filter Control for Silverlight DataGrid

by Dean 6. February 2009 14:13

During the creation of a recent demo Silverlight project, I tasked myself with creating a generic (re-useable) filter control that I could use to filter rows in a Silverlight DataGrid.

The control had to be lightweight, and work automatically with any object collection that the DataGrid was bound to – thus making it plug-and-play for any future uses.

I’ve created a great starting point with this, by designing a set of inter-operating classes that have the following key features.

  1. The Filter control of writtem completely in Xaml, using Xaml binding syntax (good designer supprt)
  2. By virtue of point 1, it is easily modified in Blend
  3. The filter control works by creating Lambda/Linq Expression trees to perform filtering.
  4. The filter control can ustilise the DescriptionAttribute for plug-and-play.

Here are a couple of screenshots:

1) No filtering

filter1

2) Adding a couple of filters, by clicking on the ‘Add New Filter’ button, setting the filter and clicking on ‘Apply All Filters’

filter2

 

As you can see, the compiled Expression tree effectively filtered the data in the main grid.

The advantages of this filter control are

  1. You could package it up, and drop it into any scenario – the only thing the filter control needs to know is the Type details of the objects in the data collection.
  2. You could play around with the visuals, and create a ‘FilteringDataGrid’ as a single control
  3. It could be a great starting point for implementing a Microsoft Excel – like ‘Auto Filter’ grid.

The ideas are limitless, and the link below will allow you to download the project and try it out for yourself.

SimpleFilterControl.zip

 

Here are some Caveats

  1. This is not production quality code. A lot of production-necessary engineering is missing for the sake of illustrating the approach(es) taken in as simple a way as possible.
  2. This code can be freely used, but comes with no guarantees whatsoever.
  3. I'm happy to hear about suggested improvements or alternative ideas, but if you have any criticisms about the quality of the code, please refer to point 1 above.

I hope those that take a look find it interesting, and I look forward to your comments.

 

Dean

Tags: ,

Silverlight | DataBinding

Comments


February 6. 2009 14:18
trackback
Trackback from DotNetKicks.com

Generic Filter Control for Silverlight DataGrid


February 7. 2009 01:26
trackback
Trackback from Community Blogs

Silverlight Cream for February 06, 2009 -- #509


February 7. 2009 08:52
pingback
Pingback from silverlight-travel.com

Silverlight Travel » Generic Filter Control for Silverlight DataGrid


Mexico Keoz 
February 22. 2009 18:17
Keoz
You saved my life too! thanks for the post


February 24. 2009 13:48
Phil Steel
Hi Dean,

Great post. I've been playing around with this to extend it to support nullable types, where for example the column/db entity could be nullable. So, in FilterExpression.cs I made a few small changes to call Nullable.GetUnderlyingType(prop.PropertyType) to check for nullable types. The code now looks like this:

//check against the underlying type in case it's nullable
Type t = Nullable.GetUnderlyingType(prop.PropertyType);
Type propertyType = t ?? prop.PropertyType;

switch(propertyType.Name)
{
    case "String":
        right = Expression.Constant(FilterValue);
        break;
    case "Int32":
        int val;
        int.TryParse(FilterValue, out val);
  right = Expression.Constant(val, prop.PropertyType); //build the expression from the original
        break;
case "DateTime":
        DateTime dt;
        DateTime.TryParse(FilterValue, out dt);
  right = Expression.Constant(dt, prop.PropertyType);
        break;
}


February 24. 2009 14:27
Dean Chalk
Phil

Thats an awesome refinement to this solution - I never considered Nullable types.

Keep up the good work my friend !

Dean

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Most comments

Tom Tom
1 comments
Derek Lakin Derek Lakin
1 comments
gb United Kingdom
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 ButtonChrome.com