Gridview filtering control - combo default

11 posts, 1 answers
  1. Indika
    Indika avatar
    6 posts
    Member since:
    Jun 2010

    Posted 22 Aug 2010 Link to this post

    Hello, 

     I have a telerik grid and I am happy with functionalities of the filtering control. By the way I want to change the default value of the filtering control combo box (now is equal to) in to "Contains"? 

    Thanking You
  2. Answer
    Rossen Hristov
    Admin
    Rossen Hristov avatar
    2477 posts

    Posted 23 Aug 2010 Link to this post

    Hello Indika,

    Here is one way to do this:

    <Window x:Class="DefaultFilterOperator.MainWindow"
            xmlns:my="clr-namespace:DefaultFilterOperator"
            Title="MainWindow" Height="700" Width="700">
        <Window.Resources>
            <my:MyViewModel x:Key="MyViewModel"/>
        </Window.Resources>
        <Grid DataContext="{StaticResource MyViewModel}">
            <telerik:RadGridView Grid.Row="0"
                                 Name="clubsGrid"
                                 ItemsSource="{Binding Clubs}"
                                 AutoGenerateColumns="False"
                                 Margin="5">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Name}"/>
                </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </Grid>
    </Window>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Telerik.Windows.Data;
    using Telerik.Windows.Controls;
    using Telerik.Windows.Controls.GridView;
     
    namespace DefaultFilterOperator
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
     
                var nameColumn = (GridViewBoundColumnBase)this.clubsGrid.Columns[0];
                var customFilteringControl = new MyFilteringControl();
                nameColumn.FilteringControl = customFilteringControl;
            }
        }
     
        /// <summary>
        /// MyFilteringControl
        /// </summary>
        public class MyFilteringControl : FilteringControl
        {
            public override void Prepare(GridViewBoundColumnBase column)
            {
                base.Prepare(column);
     
                FilteringViewModel vm = this.DataContext as FilteringViewModel;
                 
                if (vm != null)
                {
                    if (!vm.Filter1.IsActive)
                    {
                        vm.Filter1.Operator = FilterOperator.Contains;
                    }
                     
                    if (!vm.Filter2.IsActive)
                    {
                        vm.Filter2.Operator = FilterOperator.Contains;
                    }
                }
            }
        }
    }

    I hope this helps.

    Kind regards,
    Ross
    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
  3. Indika
    Indika avatar
    6 posts
    Member since:
    Jun 2010

    Posted 23 Aug 2010 Link to this post

    Hello Ross, 

     Thanks a lot for the solution. It worked well and I have managed to reached desired functionality.

     But I have another concern. My application have about 5 gridview's with having 10 columns each(approx) and also some of them will be hidden based on the business logics.

     Will there be any performance issues if I loop through gridview columns as you mentioned? or is there any other approach that I can get round of this issue?

    Thanking You..


  4. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2477 posts

    Posted 24 Aug 2010 Link to this post

    Hello Indika,

    I do not think that there will be any performance hit, but you can go ahead and try it.

    Sincerely yours,
    Ross
    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
  5. Indika
    Indika avatar
    6 posts
    Member since:
    Jun 2010

    Posted 24 Aug 2010 Link to this post

    Thank you very much for your support..
  6. Johannes
    Johannes avatar
    66 posts
    Member since:
    Dec 2012

    Posted 30 Jan 2015 Link to this post

    I'm trying to do the same thing in version Q3 2013 because I want my users to decice the default filter option.

    Unfortunately the code shown above doesn't work. When I set column.FilteringControl to a custom control inheriting from Telerik.Windows.Controls.GridView.FilteringControl like shown above, the filter popup does not open anymore.

    As I said I am using Q3-2013 and the Windows 8 theme. Can you please update the sample?
  7. Yoan
    Admin
    Yoan avatar
    1191 posts

    Posted 02 Feb 2015 Link to this post

    Hello Johannes,

    I would suggest you to follow the approach from the Change the Default Selected Filter Operators help article. Please give it a try and let me know how it works for you.

    Regards,
    Yoan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  8. Johannes
    Johannes avatar
    66 posts
    Member since:
    Dec 2012

    Posted 05 Feb 2015 in reply to Yoan Link to this post

    Hello Yoan,

    Thanks for your help, now it's working fine. However I am wondering why those DefaultFilterOperators are not implemented as Dependency Properties. So here's my extension:

    public class CustomRadGridView : RadGridView
    {
        #region Fields
     
        // Static Fields
        public static readonly DependencyProperty DefaultFilterOperator1Property;
        public static readonly DependencyProperty DefaultFilterOperator2Property;
     
        #endregion
     
        #region Constructors
     
        static CustomRadGridView()
        {
            DefaultFilterOperator1Property = DependencyProperty.Register("DefaultFilterOperator1", typeof(FilterOperator), typeof(CustomRadGridView), new PropertyMetadata(default(FilterOperator)));
            DefaultFilterOperator2Property = DependencyProperty.Register("DefaultFilterOperator2", typeof(FilterOperator), typeof(CustomRadGridView), new PropertyMetadata(default(FilterOperator)));
        }
     
        public CustomRadGridView()
        {
            FilterOperatorsLoading += OnFilterOperatorsLoading;
        }
         
        #endregion
     
        #region Properties
     
        public FilterOperator DefaultFilterOperator1
        {
            get
            {
                return (FilterOperator) GetValue(DefaultFilterOperator1Property);
            }
            set
            {
                SetValue(DefaultFilterOperator1Property, value);
            }
        }
     
        public FilterOperator DefaultFilterOperator2
        {
            get
            {
                return (FilterOperator) GetValue(DefaultFilterOperator2Property);
            }
            set
            {
                SetValue(DefaultFilterOperator2Property, value);
            }
        }
     
        #endregion
     
        #region Events
         
        private void OnFilterOperatorsLoading(object sender, FilterOperatorsLoadingEventArgs e)
        {
            // DefaultOperator1 (Row and Popup Mode)
            e.DefaultOperator1 = DefaultFilterOperator1;
     
            // DefaultOperator2 (Popup Mode only)
            if (FilteringMode == FilteringMode.Popup)
            {
                e.DefaultOperator2 = DefaultFilterOperator2;
            }
        }
     
        #endregion
     
    }


    The only part that's still not looking good is the "AND" or "OR" operator between DefaultOperator1 and DefaultOperator2. By default this value is "AND". Is there any way to change this default behavior too so it's possible to preselect a combination like "StartsWith" "OR" "EndsWith"?
  9. Yoan
    Admin
    Yoan avatar
    1191 posts

    Posted 06 Feb 2015 Link to this post

    Hello Johannes,

    You can change the logical operator per column like so:
      var columnFilter = gridview1.Columns["Name"].ColumnFilterDescriptor as IColumnFilterDescriptor;
    columnFilter.FieldFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;

    I hope this helps.

    Regards,
    Yoan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  10. Johannes
    Johannes avatar
    66 posts
    Member since:
    Dec 2012

    Posted 06 Feb 2015 in reply to Yoan Link to this post

    Hello Yoan,

    Thanks for you help. I've added another Dependeny Property called "Logical Operator" of type FilterCompositionLogicalOperator so I can set the operator in XAML. Here's my updated code behind:

    private void OnFilterOperatorsLoading(object sender, FilterOperatorsLoadingEventArgs e)
    {
        // Row and Popup Mode
        e.DefaultOperator1 = DefaultFilterOperator1;
     
        // Popup Mode only
        if (FilteringMode == FilteringMode.Popup)
        {
            // LogicalOperator
            e.Column.ColumnFilterDescriptor.FieldFilter.LogicalOperator = LogicalOperator;
     
            // DefaultOperator2
            e.DefaultOperator2 = DefaultFilterOperator2;
        }
    }



    Sadly there is no change on my UI when setting Logical Operator to "Or" instead of default value "And". If value of property LogicalOperator is "Or" that value will be written to e.Column.ColumnFilterDescriptor.FieldFilter.LogicalOperator but as I said the UI doesn't change its ComboBox to "Or". When I close the filter popup, reopen it and debug the event e.Column.ColumnFilterDescriptor.FieldFilter.LogicalOperator shows its value is still (or maybe again) "And".
  11. Yoan
    Admin
    Yoan avatar
    1191 posts

    Posted 09 Feb 2015 Link to this post

    Hi,

    Actually the LogicalOperator cannot be changed when the FilterOperatorsLoading event is raised. It is already set by this time. Please try the following code snippet and let me know about the result:
    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
     
                var columnFilter = clubsGrid.Columns["Name"].ColumnFilterDescriptor as IColumnFilterDescriptor;
                columnFilter.FieldFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;
            }


    Regards,
    Yoan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top