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

Problem with customized filtering in RadGridView

7 Answers 215 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Snype
Top achievements
Rank 1
Snype asked on 02 Mar 2012, 05:12 AM
Hi,

My requirement is to display a customized set of values for a filter rather than usual distinct value for one of the column displayed in a grid.So I handled the event DistinctValuesLoading as below.
private void OnDistinctValuesLoading(object sender, GridViewDistinctValuesLoadingEventArgs e)
{
    if(e.column == "Path")
    {
        e.ItemsSource = list;
    }
    else
    {
        e.ItemsSource = this.radGridView.GetDistinctValues(e.Column, true);
    }
}
In addition to that I also handled the Filtering event of the grid as mentioned in the code below.

The difference in behavior I found for Path column filter with respect to other columns are:
1. Clear and Filter button click doesn't trigger "Filtering" event.
2. Select All checkbox in the filter doesnot check all the listed values in my filter.
3. Clear button doesn't clear all the checked items in Path column filter

Please help.

Thanks in advance.



XML
<telerik:RadGridView Name="radGridView" ItemsSource="{Binding Documents}"
                             CanUserFreezeColumns="False"
                             AutoGenerateColumns="False"
                             ShowGroupPanel="False"
                             RowIndicatorVisibility="Collapsed"
                             DistinctValuesLoading="OnDistinctValuesLoading"
                             Filtering="OnFiltering">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Path}" UniqueName="Path"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Country}"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
         
CS code
List<String> list = new List<String>();
private void OnDistinctValuesLoading(object sender, GridViewDistinctValuesLoadingEventArgs e)
{
    if(e.column == "Path")
    {
        e.ItemsSource = list;
    }
    else
    {
        e.ItemsSource = this.radGridView.GetDistinctValues(e.Column, true);
    }
}
 
private void OnFiltering(object sender, Telerik.Windows.Controls.GridView.GridViewFilteringEventArgs e) 
        
            foreach (Telerik.Windows.Data.FilterDescriptor d in e.Removed) 
                
                    list.Remove(d.value.ToString())  ;
                
  
                foreach (Telerik.Windows.Data.FilterDescriptor d in e.Added) 
                
                    list.Add(d.value.ToString())
                
            
        }

7 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 02 Mar 2012, 09:09 AM
Hi,

What is the Type of this Path property?

Greetings,

Ross
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Snype
Top achievements
Rank 1
answered on 02 Mar 2012, 09:28 AM
Type of Path property displayed on the grid is a String.
where as the filter control display a list of path objects .

Thanks in advance.
0
Rossen Hristov
Telerik team
answered on 02 Mar 2012, 09:42 AM
Hello,

So if I understand correctly, the "original" distinct values that the column Path would display if you do not touch anything are pure strings, i.e. the Path property is of type String?

Let me try to ask the question in another way. Are the distinct values that you are supplying (strings) of the same type as the distinct values that the column would show if you don't change anything. Is the Path property on your business object of type string?

Regards,
Ross
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Snype
Top achievements
Rank 1
answered on 02 Mar 2012, 10:18 AM
The value displayed on grid view column are of type string, where as the options displayed on filter screen for that column is a collection of objects, which is completely different from the the string that I display on the grid.
Depending on the object selection on the filter screen, I wanted to filter the records on the grid. So the Filtering event for path column should checks what all objects are added to/ removed from the filter and then programatically apply the filter on that column.

I hope this clarifies.

Thanks.
0
Rossen Hristov
Telerik team
answered on 02 Mar 2012, 10:32 AM
Hi,

I am afraid that you cannot randomly change the type of the distinct values that you supply to the grid. They must be of the same type as the data displayed in the column.

What the data engine does is a Where LINQ query on the source collection. Something like this:

var results = documents.Where(document => document.Path == <<distinct value>>);

So the above Lambda expression will not work unless document.Path and <<distinct value>>  are instances of the same .NET Type.

The DistinctValuesLoading event is solely meant for limiting the distinct values amount in case it is very large, i.e. if you have thousands of distinct values and you only want to show the first 100 let's say. You should always obtain the distinct values from the RadGridView.GetDistinctValues() method. Everything else would break the data engine as I described above.

Even though I still cannot fully grasp what exactly you are trying to achieve, I strongly believe that you might need to build yourself a custom filtering control in case you want some kind of very custom filtering which is not covered by RadGridView stock filtering functionality.

If you manage to explain what exactly you are trying to achieve with these Documents and Paths and how does RadGridView's built-in filtering not suit your needs, I might be able to give you some general directions.

But before that, please, read all the topics under the Filtering section in the documentation before going on any further. They might help you solve your problem.

All the best,

Ross
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Snype
Top achievements
Rank 1
answered on 02 Mar 2012, 10:58 AM
Hi Ross,

Thanks for the reply.

As I am handling the filtering event for the grid and programatically applying filter on the displayed records, I believe, it should not call the default filter mechanism as below.
var results = documents.Where(document => document.Path == <<distinct value>>);

my customize filter in Filtering event handler is as below:
var results = documents.Where(document => document.Path contains <<distinct value>>.Path.Name);


0
Rossen Hristov
Telerik team
answered on 02 Mar 2012, 12:37 PM
Hello,

This is not how the default filtering works as I have already tried to explain.

You can't change anything in the Filtering event handler. I have already tried to explain that this event handler if for informative purposes only and cannot affect the filtering criteria sent to the data engine.

As I have already mentioned, to achieve this custom filtering scenario you will need to build a custom filtering control

You might find my blog post interesting, since it does something similar.

The sample project at the blog post is provided "as is" and is not an officially supported product -- it is just an example. You can use it in any way you find appropriate.

Thank you.

Regards,
Ross
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
GridView
Asked by
Snype
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Snype
Top achievements
Rank 1
Share this question
or