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

How to filter data when clicked on button

9 Answers 259 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Xaria D
Top achievements
Rank 1
Xaria D asked on 29 Jun 2010, 09:20 AM
I wish to filter data in a gridview upon clicking a button.
I do not want to include filtering under header column row.

P.S. I got it working


9 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 29 Jun 2010, 09:28 AM
Hi Xaria D,

Here you can find more information about filtering the grid using code.


Greetings,
Milan
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
Xaria D
Top achievements
Rank 1
answered on 29 Jun 2010, 10:48 AM
Ok I am able to filter data, but how to unfilter or rather refresh to original content
0
Yavor Georgiev
Telerik team
answered on 29 Jun 2010, 12:01 PM
Hello Xaria D,

 You simply need to clear the FilterDescriptors collection in the RadGridView. Like so:

this.radGridView1.FilterDescriptors.Clear();

All the best,
Yavor Georgiev
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
Garry Clark
Top achievements
Rank 1
answered on 07 Jul 2010, 08:32 PM
Yavor,
OK similar question bit more complex. I am basically trying to mimic the behavoir in Visual Studio Error List. Everything is working pretty well except I can't seem to figure out how to filter the grid based on my toggle button selection, like you can in Visual Studio. Also this application is following the MVVM pattern so directly interacting with the grid is really not an option.

Here is my view with my togglebuttons and the FilterDescriptors defined.
<telerikDocking:RadPane x:Class="EliteExtender.LogViewer.Views.LogViewerView" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"                
             xmlns:p="clr-namespace:EliteExtender.LogViewer.Properties" 
             xmlns:telerikSchema="http://schemas.telerik.com/2008/xaml/presentation" 
             xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
             xmlns:telerikDocking="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking" 
             xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" 
             xmlns:telerikData="clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data" 
             xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"     
             xmlns:telerikAnimation="clr-namespace:Telerik.Windows.Controls.Animation;assembly=Telerik.Windows.Controls" 
             xmlns:converters="clr-namespace:EliteExtender.Infrastructure.Converters;assembly=EliteExtender.Infrastructure" 
             xmlns:logsettings="clr-namespace:EliteExtender.LogViewer.Properties" 
             telerik:StyleManager.Theme="{Binding Source={StaticResource settings}, Path=Default.CurrentTheme}" 
             telerikDocking:RadDocking.SerializationTag="LogViewerView" Header="{Binding HeaderInfo}">      
    <telerikDocking:RadPane.Resources> 
        <converters:ByteToBooleanConverter x:Key='BitConverter' /> 
    </telerikDocking:RadPane.Resources> 
    <DockPanel> 
        <DockPanel.Resources>              
                <ResourceDictionary> 
                    <logsettings:Settings x:Key="logSettings" /> 
                </ResourceDictionary> 
        </DockPanel.Resources> 
        <telerikNavigation:RadToolBar DockPanel.Dock="Top" telerik:StyleManager.Theme="{Binding Source={StaticResource settings}, Path=Default.CurrentTheme}">  
            <ToggleButton Command="{Binding ErrorFiltercommand}" IsChecked="{Binding Source={StaticResource logSettings}, Path=Default.ErrorMessageChecked, Mode=TwoWay}">  
                <StackPanel Orientation="Horizontal">  
                    <Image Source="../Resources/Error.png" Width="16" Height="16"/>  
                    <TextBlock Text="Errors" Margin="5,0,0,0"/>  
                </StackPanel> 
            </ToggleButton> 
            <ToggleButton Command="{Binding WarningFilterCommand}" IsChecked="{Binding Source={StaticResource logSettings}, Path=Default.WarningMessageChecked, Mode=TwoWay}">  
                <StackPanel Orientation="Horizontal">  
                    <Image Source="../Resources/Warning.png" Width="16" Height="16"/>  
                    <TextBlock Text="Warnings" Margin="5,0,0,0"/>  
                </StackPanel> 
            </ToggleButton> 
            <ToggleButton Command="{Binding MessageFilterCommand}" IsChecked="{Binding Source={StaticResource logSettings}, Path=Default.InfoMessageChecked, Mode=TwoWay}">  
                <StackPanel Orientation="Horizontal">  
                    <Image Source="../Resources/Info.png" Width="16" Height="16"/>  
                    <TextBlock Text="Messages" Margin="5,0,0,0"/>  
                </StackPanel> 
            </ToggleButton> 
        </telerikNavigation:RadToolBar> 
        <telerikGrid:RadGridView x:Name="GridView" ItemsSource="{Binding LoggedEvents}" CanUserFreezeColumns="False" AutoGenerateColumns="False" IsReadOnly="True"   
                                  ShowGroupPanel="False" Margin="3" telerik:StyleManager.Theme="{Binding Source={StaticResource settings}, Path=Default.CurrentTheme}">  
            <telerikGrid:RadGridView.FilterDescriptors> 
                <telerikData:CompositeFilterDescriptor LogicalOperator="Or">  
                    <telerikData:CompositeFilterDescriptor.FilterDescriptors> 
                        <telerikData:FilterDescriptor Member="Severity" 
                             Operator="IsEqualTo" 
                             Value="Warning"               
                             IsCaseSensitive="False" /> 
                        <telerikData:FilterDescriptor Member="Severity" 
                             Operator="IsNotEqualTo" 
                             Value="Information" 
                             IsCaseSensitive="False" /> 
                        <telerikData:FilterDescriptor Member="Severity" 
                             Operator="IsEqualTo" 
                             Value="Error" 
                             IsCaseSensitive="False" /> 
                    </telerikData:CompositeFilterDescriptor.FilterDescriptors> 
                </telerikData:CompositeFilterDescriptor> 
            </telerikGrid:RadGridView.FilterDescriptors>                  
            <telerikGrid:RadGridView.Columns> 
                <telerikGrid:GridViewDataColumn Header="#" DataMemberBinding="{Binding LoggedEvents.Count}" Width="Auto"/>  
                <telerikGrid:GridViewDataColumn Header="Severity" DataMemberBinding="{Binding Severity}" Width="Auto"/>  
                <telerikGrid:GridViewDataColumn Header="Message" DataMemberBinding="{Binding Message}" Width="*"/>  
                <telerikGrid:GridViewDataColumn Header="Orginated From" DataMemberBinding="{Binding AppDomainName}" Width="Auto"/>  
                <telerikGrid:GridViewDataColumn Header="Date/Time" DataMemberBinding="{Binding TimeStampString}" SortingState="Ascending" MinWidth="100"/>  
            </telerikGrid:RadGridView.Columns> 
        </telerikGrid:RadGridView> 
    </DockPanel> 
</telerikDocking:RadPane> 
 

Any good suggestions here? I thought this would be a rather simple task, but for whatever reason I can't quite figure it out.

Thanks!
0
Yavor Georgiev
Telerik team
answered on 08 Jul 2010, 10:38 AM
Hello Garry Clark,

 I'm attaching a sample solution that accomplishes what you described.

Kind regards,
Yavor Georgiev
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
Garry Clark
Top achievements
Rank 1
answered on 08 Jul 2010, 04:02 PM
Yavor,
The sample was really great, but for whatever reason I can't quite get your solution to work. I have everything wired up and I can see the custom filter get set when the form loads, but after that point it no longer fires. I can see the ICommand fire changing the filter, but the CustomFilterDescriptor is never called.

Here is my ViewModel
using EliteExtender.Composite.Extensions;
using EliteExtender.Infrastructure;
using EliteExtender.Infrastructure.Domain;
using EliteExtender.Infrastructure.Events;
using EliteExtender.LogViewer.Properties;
using EliteExtender.LogViewer.Views;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.Unity;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
  
namespace EliteExtender.LogViewer.ViewModels
{
    internal class LogViewerViewModel : PresentationModel<ILogViewerView>
    {
        private static readonly PropertyInfo LoggedProperty = TypeManager.GetProperty<LogViewerViewModel>(x => x.LoggedEvents);
  
        private readonly IMessageService messageService;
        private readonly IUnityContainer container;
        private readonly ILoggerService loggerService;
        private readonly IEventAggregator aggregator;
  
        public LogViewerViewModel(IUnityContainer container, ILogViewerView view, IMessageService messageService, IEventAggregator aggregator, ILoggerService loggerService)
            : base(view)
        {
            this.container = container;
            this.messageService = messageService;
            this.loggerService = loggerService;
            this.aggregator = aggregator;             
  
            //Subscribe to log event so we can display the evenst that are logged by various modules
            var eventLoggedEvent = aggregator.GetEvent<LogEvent>();
            eventLoggedEvent.Subscribe(Log,true);
  
            FilterCommand = new RelayCommand(ExecuteFilter);
  
            //Log Successful Load
            loggerService.Log("Log Viewer Loaded", Microsoft.Practices.Composite.Logging.Category.Info, Microsoft.Practices.Composite.Logging.Priority.Low, 200, aggregator);
        }
  
        public string HeaderInfo
        {
            get { return Resources.LogViewerTitle; }
        }
  
        public ImageSource HeaderIcon
        {
            get { return new BitmapImage(ResourceService.GetPackUri("Resources/LogViewer.png")); }
        }
  
        private void Log(LogEntry log)
        {
            LoggedEvents.Add(log);
            LoggedEvents.DescendingSort((x, y) => x.TimeStampString.CompareTo(y.TimeStampString));
        }      
  
        private ObservableCollection<LogEntry> entries = new ObservableCollection<LogEntry>();
        public ObservableCollection<LogEntry> LoggedEvents
        {
            get
            {                
                return entries;
            }
            set
            {
                entries = value;
                RaisePropertyChanged(LoggedProperty);
            }
        }
  
        private ObservableCollection<string> filters;
        public ObservableCollection<string> Filters
        {
            get
            {
                if (filters == null)
                {
                    filters = new ObservableCollection<string>();
                }
  
                return filters;
            }
        }
  
        public ICommand FilterCommand { get; private set; }
          
        private void ExecuteFilter(object parameter)
        {
            var value = (string)parameter;
  
            if (this.Filters.Contains(value))
            {
                this.Filters.Remove(value);
            }
            else
            {
                this.Filters.Add(value);
            }
            SaveSettings();   
        }
  
        private void SaveSettings()
        {
            EliteExtender.LogViewer.Properties.Settings.Default.Save();
        }
  
        #region INotifyPropertyChanged Members
  
        public event PropertyChangedEventHandler PropertyChanged;
  
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                //this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                this.PropertyChanged(this, new PropertyChangedEventArgs(string.Empty));
            }
        }
        #endregion
    }
}

Here is my View
<telerikDocking:RadPane x:Class="EliteExtender.LogViewer.Views.LogViewerView"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              
             xmlns:p="clr-namespace:EliteExtender.LogViewer.Properties"
             xmlns:telerikSchema="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
             xmlns:telerikDocking="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking"
             xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
             xmlns:telerikData="clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data"
             xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"   
             xmlns:telerikAnimation="clr-namespace:Telerik.Windows.Controls.Animation;assembly=Telerik.Windows.Controls"
             xmlns:converters="clr-namespace:EliteExtender.Infrastructure.Converters;assembly=EliteExtender.Infrastructure"
             xmlns:logsettings="clr-namespace:EliteExtender.LogViewer.Properties"
             xmlns:filter="clr-namespace:EliteExtender.LogViewer.Domain"
             telerik:StyleManager.Theme="{Binding Source={StaticResource settings}, Path=Default.CurrentTheme}"
             telerikDocking:RadDocking.SerializationTag="LogViewerView" Header="{Binding HeaderInfo}">    
    <telerikDocking:RadPane.Resources>
        <converters:ByteToBooleanConverter x:Key='BitConverter' />
    </telerikDocking:RadPane.Resources>
    <DockPanel>
        <DockPanel.Resources>            
                <ResourceDictionary>
                    <logsettings:Settings x:Key="logSettings" />
            </ResourceDictionary>
        </DockPanel.Resources>
        <telerikNavigation:RadToolBar DockPanel.Dock="Top" telerik:StyleManager.Theme="{Binding Source={StaticResource settings}, Path=Default.CurrentTheme}">
            <ToggleButton Command="{Binding Filtercommand}" CommandParameter="Error" IsChecked="{Binding Source={StaticResource logSettings}, Path=Default.ErrorMessageChecked, Mode=TwoWay}">
                <StackPanel Orientation="Horizontal">
                    <Image Source="../Resources/Error.png" Width="16" Height="16"/>
                    <TextBlock Text="Errors" Margin="5,0,0,0"/>
                </StackPanel>
            </ToggleButton>
            <ToggleButton Command="{Binding FilterCommand}" CommandParameter="Warning" IsChecked="{Binding Source={StaticResource logSettings}, Path=Default.WarningMessageChecked, Mode=TwoWay}">
                <StackPanel Orientation="Horizontal">
                    <Image Source="../Resources/Warning.png" Width="16" Height="16"/>
                    <TextBlock Text="Warnings" Margin="5,0,0,0"/>
                </StackPanel>
            </ToggleButton>
            <ToggleButton Command="{Binding FilterCommand}" CommandParameter="Information" IsChecked="{Binding Source={StaticResource logSettings}, Path=Default.InfoMessageChecked, Mode=TwoWay}">
                <StackPanel Orientation="Horizontal">
                    <Image Source="../Resources/Info.png" Width="16" Height="16"/>
                    <TextBlock Text="Messages" Margin="5,0,0,0"/>
                </StackPanel>
            </ToggleButton>
        </telerikNavigation:RadToolBar>
        <telerikGrid:RadGridView x:Name="GridView" ItemsSource="{Binding LoggedEvents}" CanUserFreezeColumns="False" AutoGenerateColumns="False" IsReadOnly="True" 
                                  ShowGroupPanel="False" Margin="3" telerik:StyleManager.Theme="{Binding Source={StaticResource settings}, Path=Default.CurrentTheme}">
            <telerikGrid:RadGridView.FilterDescriptors>
                <filter:CustomFilterDescriptor Member="Severity">
                    <filter:CustomFilterDescriptor.Filter>
                        <filter:FilterValues Values="{Binding Filters}" />
                    </filter:CustomFilterDescriptor.Filter>
                </filter:CustomFilterDescriptor>
            </telerikGrid:RadGridView.FilterDescriptors
            <telerikGrid:RadGridView.Columns>
                <telerikGrid:GridViewDataColumn Header="#" DataMemberBinding="{Binding LoggedEvents.Count}" Width="Auto"/>
                <telerikGrid:GridViewDataColumn Header="Severity" DataMemberBinding="{Binding Severity}" Width="Auto"/>
                <telerikGrid:GridViewDataColumn Header="Message" DataMemberBinding="{Binding Message}" Width="*"/>
                <telerikGrid:GridViewDataColumn Header="Orginated From" DataMemberBinding="{Binding AppDomainName}" Width="Auto"/>
                <telerikGrid:GridViewDataColumn Header="Date/Time" DataMemberBinding="{Binding TimeStampString}" SortingState="Ascending" MinWidth="100"/>
            </telerikGrid:RadGridView.Columns>
        </telerikGrid:RadGridView>
    </DockPanel>
</telerikDocking:RadPane>

and the CustomFilterDescriptor and RelayCommand classes are exactly the same as yours just different namespaces. Also maybe to note that I am using Prims to do my DI so the Datacontext gets wired up there and everythign seems to be wired OK as all my other bindings do work.

If you can see something wrong please let me know.

Thanks as always!
0
Yavor Georgiev
Telerik team
answered on 08 Jul 2010, 04:09 PM
Hello Garry Clark,

 Could you debug the event handler in CustomFilterDescriptor and FilterValue and see if they fire?

Regards,
Yavor Georgiev
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
Garry Clark
Top achievements
Rank 1
answered on 08 Jul 2010, 05:51 PM
Yavor,
No the only thing that fires at all in the CustomFilterDescriptor is this properties set.
public string Member
       {
           get
           {
               return this.member;
           }
           set
           {
               if (value != this.member)
               {
                   this.member = value;
                   this.OnPropertyChanged("Member");
               }
           }
       }

and it is only set when the view Initializes. Should I open a ticket and just attach a small project with this module included?

Thanks for such a quick response by the way.
0
Yavor Georgiev
Telerik team
answered on 08 Jul 2010, 09:36 PM
Hello Garry Clark,

 That would be best, it's not possible for me to recreate your Composite Application structure on my own.

Kind regards,
Yavor Georgiev
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
Tags
GridView
Asked by
Xaria D
Top achievements
Rank 1
Answers by
Milan
Telerik team
Xaria D
Top achievements
Rank 1
Yavor Georgiev
Telerik team
Garry Clark
Top achievements
Rank 1
Share this question
or