Telerik blogs

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.


Comments

Comments are disabled in preview mode.