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

GridView ColumnFilter option

18 Answers 294 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Amit Patel
Top achievements
Rank 1
Amit Patel asked on 20 Apr 2011, 08:57 PM
Hello,
I have filter enabled for all of my grid view columns, and would like to change default filter option from "Is Equal to" to "Contains"  How can I achinve this?

Thanks
Amit

18 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 21 Apr 2011, 09:50 AM
Hello Amit Patel,

You may customize the filtering control by creating one of your own and change the initial selected value inside the two combo boxes in the filter. It may be like follows:

public class CustomizedFilteringControl : FilteringControl
    {
        public override void Prepare(GridViewBoundColumnBase column)
        {
            base.Prepare(column);
 
            var availableOperators = new List<FilterOperator>()
            {
                FilterOperator.Contains,
                FilterOperator.IsEqualTo,
                FilterOperator.IsNotEqualTo,   
                FilterOperator.StartsWith,
                FilterOperator.EndsWith,
                FilterOperator.DoesNotContain,
                FilterOperator.IsContainedIn,
                 
            };
 
            var cbo1 = this.ChildrenOfType<RadComboBox>()
            .Where(b => b.Name == "PART_Filter1ComboBox")
            .FirstOrDefault();
            cbo1.SelectedValue = availableOperators.FirstOrDefault();
            cbo1.ItemsSource = availableOperators;
 
            var cbo2 = this.ChildrenOfType<RadComboBox>()
            .Where(b => b.Name == "PART_Filter2ComboBox")
            .FirstOrDefault();
            cbo2.SelectedValue = availableOperators.FirstOrDefault();
            cbo2.ItemsSource = availableOperators;
        }
    }
 

You may define the filtering control for the required column in xaml, in the code-behind or during the AutoGeneratingColumn event of the grid:

XAML:
<telerik:GridViewDataColumn DataMemberBinding="{Binding Name}">
            <telerik:GridViewDataColumn.FilteringControl>
                <my:CustomizedFilteringControl />
            </telerik:GridViewDataColumn.FilteringControl>
</telerik:GridViewDataColumn>
 
C#:
var nameColumn = (GridViewBoundColumnBase)this.clubsGrid.Columns["Name"];
var customFilteringControl = new CustomizedFilteringControl();
nameColumn.FilteringControl = customFilteringControl ;


Greetings,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Dominik
Top achievements
Rank 1
answered on 31 Aug 2011, 02:31 PM
HI

The posted solution does not work properly. At first it seems ok but somehow but when you reopen the filter i have encountered the following problems:

a) the displayed selection has changed
b) the grid is apparently filtered by another selected that does neither matches the one displayed or originally set.

This is an important issue for us as we are using the GridView control in combination with the domain datasource and ria services. The filter operators "IsContainedIn" and IsNotContained in will result in an exception. Please help.

thanks in advance



The following approach has the same problems
public override void Prepare(GridViewBoundColumnBase column)
      {
          base.Prepare(column);
 
          if (column.DataType == typeof(string))
          // Set contains as default only for string data type of the gridview column
          {
 
                 var vm = this.DataContext as FilteringViewModel;
              if (vm != null)
              {
                  if (vm.AvailableActions.Contains(FilterOperator.IsNotContainedIn))
                  {
                      vm.AvailableActions.Remove(FilterOperator.IsNotContainedIn);
                  }
                  if (vm.AvailableActions.Contains(FilterOperator.IsContainedIn))
                  {
                      vm.AvailableActions.Remove(FilterOperator.IsContainedIn);
                  }
              }
0
Maya
Telerik team
answered on 31 Aug 2011, 03:21 PM
Hi Dominik,

You can remove the setting of the selected value of the combo box:

var cbo1 = this.ChildrenOfType<RadComboBox>()
            .Where(b => b.Name == "PART_Filter1ComboBox")
            .FirstOrDefault();    
cbo1.ItemsSource = availableOperators;


 

All the best,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Dominik
Top achievements
Rank 1
answered on 05 Sep 2011, 09:47 AM
Hi

I almost thought that i have found the solution. Just temporarily store the value in a variable and reassign it at the end (makes kind of sense if there is a two-way binding between the viewmodel and the control):
public class DefaultFilter : FilteringControl
   {
       public override void Prepare(GridViewBoundColumnBase column)
       {
           base.Prepare(column);
 
           if (column.DataType == typeof(string))
           // Set contains as default only for string data type of the gridview column
           {
               var vm = this.DataContext as FilteringViewModel;
               if (vm != null)
               {
 
                   var filter1 = vm.Filter1.Operator; // save filter value in a variable as they are changed when bound to the combobox (probably a two-way binding)
                   var filter2 = vm.Filter2.Operator;
 
                   // approach recommended by telerik : http://www.telerik.com/community/forums/silverlight/gridview/gridview-columnfilter-option.aspx
                   var availableOperators = new List<FilterOperator>()
                                                {
                                                    FilterOperator.IsEqualTo,
                                                    FilterOperator.IsNotEqualTo,
                                                    FilterOperator.StartsWith,
                                                    FilterOperator.EndsWith,
                                                    FilterOperator.Contains,
                                                    FilterOperator.DoesNotContain,
                                                };
 
                   var comboBox1 = this.ChildrenOfType<RadComboBox>()
                       .Where(b => b.Name == "PART_Filter1ComboBox")
                       .FirstOrDefault();
                   comboBox1.SelectedValue = availableOperators.FirstOrDefault();
                   comboBox1.ItemsSource = availableOperators;
 
 
 
                   var comboBox2 = this.ChildrenOfType<RadComboBox>()
                       .Where(b => b.Name == "PART_Filter2ComboBox")
                       .FirstOrDefault();
                   comboBox2.SelectedValue = availableOperators.FirstOrDefault();
                   comboBox2.ItemsSource = availableOperators;
                    
                   if (vm.Filter1.IsActive)
                   {
                       comboBox1.SelectedValue = filter1;
                   }
                   if (vm.Filter2.IsActive)
                   {
                       comboBox2.SelectedValue = filter2;
                   }


However, this doews not fully work!!

How to reproduce:
- 1. Set filter settings (Filter 1: Equeals to a certain value, "OR"-Setting, Filter2: Equeals to another value) -> this will filter correctly
- 2. Reopen filter-> filter will start to behave strangely: filtering in grid is going to be flawed. Close and reopen filter-> displayed values are wrong

My guess is, that i would have to have to reapply not only the operator values to the two comboboxes but also all other values.
How can i make the gridview filter correctly? To me, this seems to be a major issue. Filtering is a core functionality!

Thanks in advance


By the way: we are using Silverlight 4, Ria Services (which is why we had to remove the ContainIn-Stuff), and the latest release of telerik controls.
0
Maya
Telerik team
answered on 05 Sep 2011, 11:35 AM
Hi Dominik,

May you clarify why the suggested approach is not appropriate for you and you want to extend its functionality ? What is the behavior you want to achieve ? 
 

Kind regards,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Dominik
Top achievements
Rank 1
answered on 05 Sep 2011, 02:19 PM

Hi

If you use the adapted default filter (see my pasted code for the Prepare(GridViewBoundColumnBase column) method, you just have to set filtersettings (for filter 1 and filter 2) and then close and reopen the filter a couple of times and you will see that filtering does not work any longer. What do I have to do to make this work? It is actually pretty easy to reproduce.

PS 1: We use ria services, silverlight 4 and the latest release of silverlight controls.


PS 2:  Ive just seen that the Operators "IsContainedIn" and "IsNotContainedIn" now work also for if the grid is bound to a ria service method. This used to be different. This would allow us to go with the default filter, although i am not happy with it as the "contained in" operators are confusing to the user.
0
Maya
Telerik team
answered on 08 Sep 2011, 12:25 PM
Hi Dominik,

Unfortunately, I am not quite sure why you are extending the functionality of the Prepare method in such a manner. What is the behavior that you want to get ?
Indeed, the code provided by you will cause some undesired behavior, but in fact that is the expected one, since you are trying to force the setting of the SelectedValue of the RadComboBox-es. You need to leave that job to the filter itself. Thus once you filter and reopen the filtering control, the correct value will be displayed in the combo boxes.
Considering the two operators - "Is contained in" and "Is not contained in" - their presence depends on the type of the property bound to the corresponding column, not on whether you are calling a particular method. Why do you consider them confusing, what will be the behavior you expect when using them ? 
 

Kind regards,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Dominik
Top achievements
Rank 1
answered on 14 Sep 2011, 01:39 PM
Hi

I dont really think that the two operators - "Is contained in" and "Is not contained in" - are that confusing, but my customer and the people who have been testing (the endusers) seem to have problem with it.
If there is a simple way of getting rid of them without any side effects, i would be glad. If this is not so easily possible, it is also ok.

Greetings
0
Maya
Telerik team
answered on 14 Sep 2011, 04:12 PM
Hi Dominik,

The way to go for the time being would be to create your own custom filtering control (just as illustrated in this forum thread). Still, I am sending you a sample project illustrating a working solution, implementing the suggested approach.


 

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Christoph
Top achievements
Rank 1
answered on 11 Nov 2011, 04:49 PM
Thanks for that example it works great.

But for some reason I somethimes do not see the filter funnel turned on when I filtered my data. This is specially annoying when you close the filter control because then you don't know which column you should clear to get all data again.

I already checked the forum (e.g. http://www.telerik.com/community/forums/silverlight/gridview/force-the-funnel-icon-of-filtering-control-to-light-up.aspx) but could not yet find an answer how I can set the funnel manually which I don't think is a good solution but for the moment the only one I see.

Thanks for your help. Cheers,
 christoph
0
Missing User
answered on 13 Mar 2012, 02:05 PM
Hy Maya.

I have already created my custom Filter using your approach described here in this post. Today we've decided to Update or Controls to the newest version (Q1 2012). Now I'm facing some problems, I saw at the documentation that you need to do some changes in this new version, but i couldn't manage to found a solution just reading there. Would you please provide some way to fix the Filtering in this new version? Here is my code:

public class CustomFilteringControl : FilteringControl
    {       
        public override void Prepare(GridViewColumn gridcolumn)
        {
            base.Prepare(gridcolumn);
 
            var vm = this.DataContext as FilteringViewModel;
 
            GridViewBoundColumnBase column = gridcolumn as GridViewBoundColumnBase;
 
            if (vm != null)
            {
                if (!vm.Filter1.IsActive)
                {
                    if (column.DataType.ToString().Contains("System.Int"))
                    {
                        vm.Filter1.Operator = FilterOperator.IsEqualTo;
                    }
                    else
                    {
                        vm.Filter1.Operator = FilterOperator.Contains;
                    }
                }
 
                if (!vm.Filter2.IsActive)
                {
                    if (column.DataType.ToString().Contains("System.Int"))
                    {
                        vm.Filter2.Operator = FilterOperator.IsEqualTo;
                    }
                    else
                    {
                        vm.Filter2.Operator = FilterOperator.Contains;
                    }
                }
            }
        }
    }

Thanks.
0
Rossen Hristov
Telerik team
answered on 13 Mar 2012, 02:11 PM
Hello,

I have some very good news. There is no longer a need to inherit the stock FilteringControl simply to change the default operator. That is just too complex.

RadGridView has an event called FilterOperatorsLoading. In the event arguments you have the column for which the operators are being loaded. You can remove some of them if you want to. You can also specify the default operator to be selected when the end-user opens the filtering control. It's all there in the event arguments.

Let me know if there are problems.

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
Missing User
answered on 13 Mar 2012, 03:41 PM
Fantastic!
Thank you Ross for your quick response, with this new event i was able to set a default filter in a much easier way, just perfect.

Maybe you guys can update the documentation for further orientation, will help a lot of people.

Best regards.



0
Reimund
Top achievements
Rank 1
answered on 03 Apr 2012, 08:15 AM
the example code works great, except for the first call.

It would be better to change the order  of the following lines
cbo1.SelectedValue = availableOperators.FirstOrDefault();
cbo1.ItemsSource = availableOperators;

to
 cbo1.ItemsSource = availableOperators;
      cbo1.SelectedValue = availableOperators.FirstOrDefault();


Same change for cbo2 too.

Reimund
0
David Ocasio
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 19 Apr 2012, 05:51 PM
bug report

the defaults are not honored if you hit the clear filters button.
Private Sub RadGridView_FilterOperatorsLoading(sender As System.Object, e As Telerik.Windows.Controls.GridView.FilterOperatorsLoadingEventArgs)
    If e.Column.UniqueName = "Message" Then
        e.DefaultOperator1 = Telerik.Windows.Data.FilterOperator.Contains
        e.DefaultOperator2 = Telerik.Windows.Data.FilterOperator.DoesNotContain
    End If
End Sub

0
Rossen Hristov
Telerik team
answered on 20 Apr 2012, 09:16 AM
Hi,

Can you please tell us the exact version that you are using?

Regards,
Ross
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
David Ocasio
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 20 Apr 2012, 12:48 PM
latest released
2012.1.326.1050
Included snapshot for your consumption
lol.

Thanks
dco

0
Rossen Hristov
Telerik team
answered on 23 Apr 2012, 10:59 AM
Hello,

We fixed the issue and it will be working properly in the next Latest Internal Build.

Kind regards,
Ross
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Amit Patel
Top achievements
Rank 1
Answers by
Maya
Telerik team
Dominik
Top achievements
Rank 1
Christoph
Top achievements
Rank 1
Missing User
Rossen Hristov
Telerik team
Reimund
Top achievements
Rank 1
David Ocasio
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or