Pre-filtering RadGridView for Silverlight

by XAML Team | Comments 13

In the last couple of weeks we have received several requests for a new feature. Imagine that you want to display RadGridView already filtered by a certain criteria. You could always do this in the past by writing something like this:

   1: <telerik:RadGridView Name="playersGrid">
   2: <telerik:RadGridView.FilterDescriptors>
   3: <telerikData:FilterDescriptor Member="Country" Operator="IsEqualTo" Value="England"/>
   4: </telerik:RadGridView.FilterDescriptors>
   5: </telerik:RadGridView>

 

Now, that is perfectly legal and the grid will come up filtered. The filtering UI however, will not be aware of this. Expressed in other words, this feature might sound like this: “I would like to programmatically achieve the same effect that is achieved by filtering the grid with the mouse/keyboard through the UI.”

This is now possible. You will need to work with a special IFilterDescriptor which is recognizable by our UI. Its name is ColumnFilterDescriptor and it represents what you are already familiar with – the distinct values list and the two filters on the bottom of the filtering UI. In order to create it you will need to pass the column of interest as its constructor’s sole parameter. So for example, if you write something like this:

   1: using System.Windows.Controls;
   2: using Telerik.Windows.Controls;
   3: using Telerik.Windows.Data;
   4: using Telerik.Windows.Controls.GridView;
   5:  
   6: namespace PrefilteringRadGridView
   7: {
   8: public partial class MainPage : UserControl
   9:     {
  10: public MainPage()
  11:         {
  12:             InitializeComponent();
  13:  
  14: // Get only players with a number less than 10
  15:             ColumnFilterDescriptor numberColumnFilter = 
  16: new ColumnFilterDescriptor((IDataFieldDescriptor)this.playersGrid.Columns[1]);
  17:             numberColumnFilter.FieldFilter.Filter1.Operator = FilterOperator.IsLessThan;
  18:             numberColumnFilter.FieldFilter.Filter1.Value = 10;
  19: this.playersGrid.FilterDescriptors.Add(numberColumnFilter);
  20:  
  21: // Get only players from England and Spain
  22:             ColumnFilterDescriptor countryColumnFilter = 
  23: new ColumnFilterDescriptor((IDataFieldDescriptor)this.playersGrid.Columns[2]);
  24:             countryColumnFilter.DistinctFilter.DistinctValues.Add("England");
  25:             countryColumnFilter.DistinctFilter.DistinctValues.Add("Spain");
  26: this.playersGrid.FilterDescriptors.Add(countryColumnFilter);
  27:  
  28: this.playersGrid.ItemsSource = Club.GetPlayers();
  29:         }
  30:     }
  31: }

 

When the grid is loaded it will display players with a Number less than 10 who are from either England or Spain. Furthermore, when you click on these two columns’ filtering funnels you will see the correct values loaded:

 

Here is the full source code. You will need to get the Latest Internal Build coming out by the end of the day.

13 Comments

Ben Hayat
Perhaps I'm missing something obvious, but if you're using RIA services and perhaps RIA DataSource, wouldn't it make more sense to do the filtering on the RIA side to only return the desired objects over the wire?

What would be a solid reason to request more data from server to client and do the filtering on client?
Thanks!
..Ben
Rossen Hristov
Hello Ben,

You are absolutely right. Here is our online example with RIA Services that does just what you have described.

By the way, if you create a brand new custom filtering control, as described in my previous blog post, you can make this control communicate directly with the server and bypass RadGridView's filters altogether. This would be really powerful I think.
Ben Hayat
Thanks for the confirmation;

=>You are absolutely right. Here is our online example with RIA Services that does just what you have described.<=

Excellent demo!

=>By the way, if you create a brand new custom filtering control, as described in my previous blog post, you can make this control communicate directly with the server and bypass RadGridView's filters altogether. This would be really powerful I think.<=

Although I had not read the previous blog, but I was thinking of the exact thing of bypassing the Grid's filter altogether. In RIA cases, especially when data is bening changed by other users, it's best to do server side filtering.

Thank you!
..Ben
Tooraj
Hi,

I created this sample on Silverlight 4, it does filter the grid but the filter icon is black (like no filter set) also when I open the filter window all checkboxes are unchecked! Am I missing something?

Thanks.
Tooraj
Tooraj
Hi,

I created this sample on Silverlight 4, it does filter the grid but the filter icon is black (like no filter set) also when I open the filter window all checkboxes are unchecked! Am I missing something?

Thanks.
Tooraj
Tooraj
Hi,

I created this sample on Silverlight 4, it does filter the grid but the filter icon is black (like no filter set) also when I open the filter window all checkboxes are unchecked! Am I missing something?

Thanks.
Tooraj
Oxy
How programmatically to clear this column filter?
Rossen Hristov
Hello Oxy,

Simply remove it from the collection that contains it.

Let me know if you have any other questions.
Oxy
After removing FilterDescriptor from the collection, Column filter doesn't get populated with distinct values. How to leave all the values in the Column filter drop down, just make them unchecked?
Rossen Hristov
That sounds strange. Could you please open a support ticket, attach a sample project and I will take a look at it. Thanks.
Oxy
I found why it was happening.  It was using another filter which coused the problem. So, removing filter from the collection works fine.
Oxy
I found why it was happening.  It was using another filter which coused the problem. So, removing filter from the collection works fine.
Deon
Does anyone know if this is still applicable? 
In the sample provided, the constructor is expecting an IDataFieldDescriptor, while in the current version (2012.2) it is expecting a GridViewColumn.

            

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by XAML Team - or - read latest articles in Developer Tools