This is a migrated thread and some comments may be shown as answers.

Setting a column filter from the code-behind

6 Answers 528 Views
GridView
This is a migrated thread and some comments may be shown as answers.
GEB
Top achievements
Rank 1
GEB asked on 19 Feb 2009, 04:27 PM
Do you have a sinple example of how to set a filter for a specific column using the latest RadGridView from the code-behind?

Also, in my application, I anticipate up to 500K rows (when there is no filter), and approximately 50-75 columns (most hidden at any given time).  Do you anticipate any performance challenges with this many rows and columns with the new RadGridView?

6 Answers, 1 is accepted

Sort by
0
Jordan
Telerik team
answered on 23 Feb 2009, 04:21 PM
Hi GEB,

You can set the filter in the code behind by using FieldFilterDescription and CompositeFilterDescription.
You have to set one of them to RadGridView FilterDescription property.

FieldFilterDescription has 2 properties:
  • FieldName - the name of the property
  • Values - collection of values that are filtered
CompositeFilterDescription is collection of FilterDescriptions.

Sample code follows:
<telerik:RadGridView Name="radGridView1" AutoGenerateColumns="True" Height="500" /> 

public partial class Page : UserControl 
    { 
        public Page() 
        { 
            InitializeComponent(); 
            this.radGridView1.ItemsSource = this.GetPersons(); 
 
            FieldFilterDescription filterDescriptionAge = new FieldFilterDescription(); 
            filterDescriptionAge.FieldName = "Age"
            filterDescriptionAge.Values.Add(1); 
 
            FieldFilterDescription filterDescriptionName = new FieldFilterDescription(); 
            filterDescriptionName.FieldName = "Name"
            filterDescriptionName.Values.Add("10"); 
 
            CompositeFilterDescription compositeFilterDescription = new CompositeFilterDescription(); 
            compositeFilterDescription.FilterDescriptions.Add(filterDescriptionAge); 
            compositeFilterDescription.FilterDescriptions.Add(filterDescriptionName); 
 
            this.radGridView1.FilterDescription = compositeFilterDescription; 
        } 
 
        public IEnumerable<Person> GetPersons() 
        { 
            return from i in Enumerable.Range(1, 200) 
                   select new Person { Name = (i / 10).ToString(), Age = i % 10 }; 
        } 
    } 
 
    public class Person 
    { 
        public string Name { getset; } 
        public int Age { getset; } 
    } 

You can also create your custom field description class. You have to inherit base FilterDescription class.

You will have performance issues with so many rows and columns. It will be much better if you consider implementing some kind of paging.
Look at this blog post.

All the best,
Jordan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
GEB
Top achievements
Rank 1
answered on 24 Feb 2009, 05:10 AM
I received a new version of the RadGridView last week.  In my version, there is no 'Values' member for a FieldFilterDescription object.  There is a 'DistinctValues' member, but since there is no documentation, I'm not certain how to use it.  Can you resend a simple example that does the following:

1. There is a column in the RadGridView named "Category"
2. I want to filter for all values of Category equal to "ABC" or "DEF"

I tried the following, but it does not work:

        private void PageLoaded(object sender, RoutedEventArgs e)  
        {  
            FieldFilterDescription filterDescriptionCategory = new FieldFilterDescription();  
            filterDescriptionCategory.FieldName = "Category";  
            FilterDescriptor filterDescriptorABC = new FilterDescriptor("Category", FilterOperator.IsEqualTo, "ABC");  
            filterDescriptionCategory.DistinctValues.Add(filterDescriptorABC);  
            FilterDescriptor filterDescriptorDEF = new FilterDescriptor("Category", FilterOperator.IsEqualTo, "DEF");  
            filterDescriptionCategory.DistinctValues.Add(filterDescriptorDEF);  
 
            this.radGridView1.FilterDescription = filterDescriptionCategory;    
        }  
 

The ItemsSource for radGridView1 was set before the PageLoaded() method was called, and was set to an ObserveableCollection.  My grid shows all of the rows, as opposed to only those with values of "ABC" or "DEF".  I.e., no filtering is being performed.


0
Valeri Hristov
Telerik team
answered on 24 Feb 2009, 10:34 AM

Hello GEB,

The proper code is:
private void PageLoaded(object sender, RoutedEventArgs e)
{
 FieldFilterDescription filterDescriptionCategory = new FieldFilterDescription();
 filterDescriptionCategory.FieldName = "Category";
 filterDescriptionCategory.DistinctValues.Add(new FilterDescriptor("Category", FilterOperator.IsEqualTo, "ABC"));
 filterDescriptionCategory.DistinctValues.Add(new FilterDescriptor("Category", FilterOperator.IsEqualTo, "DEF"));

 // Apply distinct filter
 filterDescriptionCategory.SelectedValues.Add(filterDescriptionCategory.DistinctValues[0]);

 this.radGridView1.FilterDescription = filterDescriptionCategory;
}

The new FieldFilterDescription class is a composite filter, that has the ability to filter depending on several parameters:
DistinctValues is the collection that is used to populate the checkbox list in the filtering dropdown
SelectedValues is the collection, containing the checked items from the above list. It is the actual filter.
Filter1 is the first filter, below the checkbox list
Filter2 is the second filter.

If you want to display many predefined items, you should use the SelectedValues. Otherwise, I would recommend using Filter1 and Filter2, e.g. the code above would be:
 FieldFilterDescription filterDescriptionCategory = new FieldFilterDescription();
 filterDescriptionCategory.FieldName = "Category";
 filterDescriptionCategory.Filter1.Operator = FilterOperator.IsEqualTo;
 filterDescriptionCategory.Filter1.Value = "ABC";

 this.radGridView1.FilterDescription = filterDescriptionCategory;

Greetings,

Valeri Hristov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
GEB
Top achievements
Rank 1
answered on 24 Feb 2009, 01:48 PM
Thank you for your quick response.  However, your new example does not work in my application.  Firstly, I had to set:

filterDescriptionCategory.DataType =

typeof(String);

Otherwise, I would get a null reference when I tried to apply the filter to the grid.  Secondly, your example successfully filters for "ABC", but none of the rows where the Category is "DEF" are displayed.  It seems to have filtered for the first value, but not for the second.  I'm looking for a filter that essentially displays the row if they are "ABC" or "DEF".

 

0
GEB
Top achievements
Rank 1
answered on 24 Feb 2009, 02:09 PM
Also, if I add:

filterDescriptionCategory.SelectedValues.Add(filterDescriptionCategory.DistinctValues[1]);

to add the second filter to the SelectedValues, then no filtering is applied and all categories are shown in the grid.  So, using this example code, it appears as though I can show use one of the filtered values, but not both.

Note that in my application, there are times where I might want to apply 10 or more filtering values to a single column.  For example, I might want to find "ABC" or "DEF" or "GHI" ... So Filter1 and Filter2 that you referred to will not wor in my case.

0
Valeri Hristov
Telerik team
answered on 25 Feb 2009, 10:43 AM
Hi Gary,

The distinct filters should do the job. However, there is a small optimization in the filter logic, that does not apply the filter if the SelectedValues collection is the same as DistinctValues collection (this is when you select all checkboxes in the UI, which contain ALL values in the column). So to resolve the problem I would recommend either adding a dummy value in the DistinctValues, or add all distinct values from the column in that collection.

Greetings,
Valeri Hristov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
GEB
Top achievements
Rank 1
Answers by
Jordan
Telerik team
GEB
Top achievements
Rank 1
Valeri Hristov
Telerik team
Share this question
or