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

Problem with compositeFilter.LogicalOperator

3 Answers 76 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Snype
Top achievements
Rank 1
Snype asked on 21 Mar 2012, 02:23 PM
XML file for the Cusom Filter Control
 <TextBlock
                Grid.Row="0"
                 Margin="2" 
                Text="Show Attributes of Discipline" />
            <ListBox
                x:Name="m_oDisciplines"
                Grid.Row="1">   
            </ListBox>
            <StackPanel Orientation="Horizontal"
                        Grid.Row="2">
                 <telerik:RadButton
                     Name="m_oFilterButton"
                    Content="Filter"
                    Click="m_oFilterButton_Click"
                    Margin="2"
                    Width="80" />
                <telerik:RadButton
                    Name="m_oClearButton"
                    Content="Clear" 
                    Click="m_oClearButton_Click"
                    Margin="2"
                     Width="80" />
            </StackPanel>
  
.CS file portion for the filter control is as below:
   
public DisciplineFilterCtrl()
        {
            InitializeComponent();
            CreateListBoxWithCheckBox(MetaDataCache.Cache.Disciplines);
        }
   
        private void CreateListBoxWithCheckBox(ObservableCollection<DisciplineWrapper> list)        { 
            List<ListBoxItem> listBoxItem = new List<ListBoxItem>();
            foreach (DisciplineWrapper item in list)
            {
                if (item.Id != Guid.Empty && !item.Deleted)
                {
                     ListBoxItem tempItem = new ListBoxItem();
                    CheckBox m_oCheckBox = new CheckBox();
                    m_oCheckBox.Content = item.Name;
                    m_oCheckBox.Tag = item; 
                    tempItem.Content = m_oCheckBox;
                    listBoxItem.Add(tempItem); 
                }
            }
            m_oDisciplines.ItemsSource = listBoxItem; 
        }
  
 private CompositeFilterDescriptor compositeFilter;
 private Telerik.Windows.Controls.GridViewBoundColumnBase column;
 private Telerik.Windows.Data.FilterDescriptor[] DisciplineFilter;  
private void CreateFilters() 
        {
            compositeFilter = new CompositeFilterDescriptor();
            compositeFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;
            int count = MetaDataCache.Cache.Disciplines.Count;
            string dataMember = this.column.DataMemberBinding.Path.Path;
            DisciplineFilter = new FilterDescriptor[count];
            for (int i = 0; i < count; i++)
            {
                 this.DisciplineFilter[i] = new Telerik.Windows.Data.FilterDescriptor(dataMember                         , Telerik.Windows.Data.FilterOperator.Contains
                         , null);
                compositeFilter.FilterDescriptors.Add(DisciplineFilter[i]);
            }
             compositeFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;
   
        }
   
        public void Prepare(Telerik.Windows.Controls.GridViewBoundColumnBase column)        {
   
            this.column = column as Telerik.Windows.Controls.GridViewBoundColumnBase;
            if (this.column == null)
            {
                return;
            }
             if (this.compositeFilter == null)
            {
                   CreateFilters();
   
            }
        }
   
        private void FilterRecord()
        {
            // this.compositeFilter.FilterDescriptors.Clear();
            this.column.DataControl.FilterDescriptors.Clear();
            int i = 0;
            List<ListBoxItem> listItems = this.m_oDisciplines.ItemsSource as List<ListBoxItem>;
            if (null != listItems)
             {
                 foreach (var item in listItems)
                {
                     CheckBox m_oCheckBox = item.Content as CheckBox;
                    if (null != m_oCheckBox && m_oCheckBox.IsChecked.Value) 
                    {
                        //this.compositeFilter.FilterDescriptors.Add(DisciplineFilter[i]);
                        this.DisciplineFilter[i].Value = m_oCheckBox.Content;
                         this.column.DataControl.FilterDescriptors.Add(DisciplineFilter[i]);
                        i++;
                    }
                 
            
        }
   
 private void m_oFilterButton_Click(object sender, RoutedEventArgs e)        { 
            FilterRecord(); 
            this.IsActive = true; 
        }

Hi,

I have created a custom filter control. In The filter control I am displaying a list of available options for the associated column.
The column may have a values separated by comma or newline.
For Example a Column value is "Electrical, Mechanical". The filter control has options listed as "Electrical" "Mechanica", "HVAC"..etc. If I select "Electrical" and "Mechanical" in the custom filter screen and click Filter, It should display me all the rows that contains "Electrical" or "Mechanical".

To achive this I have used a composite filter and set the logical operator to FilterCompositionLogicalOperator.Or
But it stil gives me the resulting rows where the column contains "Electrical" AND "Mechanical". Is there any way I could make it to display rows contaings data as "Electrical" OR "Mechanical".

Data Type for the source column is string.

Please help.
Thanks in advance

3 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 21 Mar 2012, 02:46 PM
Hi,

That sounds strange.

Can you please send us a small runnable dummy project with this sample data and the composite filter that you are building. You don't have to do this in a custom filter control -- you can directly add the composite filter to RadGridView.FilterDescriptors from the code-behind of the page.

You can hard-code everything that is not relevant.

We are looking forward to hearing from 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 >>
0
Snype
Top achievements
Rank 1
answered on 21 Mar 2012, 04:42 PM
Hi,
Please find the complete code for my dummy project as below. I am not able to find a convinient way to attach a zip file for my dummy project to this reply. 
MainPage.xml
 
<UserControl x:Class="DummyProject.MainPage"
    xmlns:local="clr-namespace:DummyProject"
    mc:Ignorable="d"
    xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls"
    d:DesignHeight="300" d:DesignWidth="400">
 
    <Grid x:Name="LayoutRoot" Background="White">
        <telerik:RadGridView
            x:Name="m_oRadGridView"
            AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn
                    Header="Name"
                    DataMemberBinding="{Binding Name}" />
                <telerik:GridViewDataColumn
                    Header="Disciplines"
                    ShowDistinctFilters="true"
                    DataMemberBinding="{Binding DisciplineDescription}">
                    <telerik:GridViewDataColumn.FilteringControl>
                        <local:DisciplineFilterCtrl />
                    </telerik:GridViewDataColumn.FilteringControl>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>
 
 
 
MainPage.xml.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
 
namespace DummyProject
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            ObservableCollection<MainType> list = new ObservableCollection<MainType>();
            list.Add(new MainType("Attr1", "Discipline1, Discipline2, Discipline3"));
            list.Add(new MainType("Attr2", "Discipline1, Discipline2, Discipline3"));
            list.Add(new MainType("Attr3", "Discipline1, Discipline2, Discipline3"));
            list.Add(new MainType("Attr4", "Discipline1, Discipline2, Discipline3"));
            list.Add(new MainType("Attr5", "Discipline1, Discipline2, Discipline3"));
            list.Add(new MainType("Attr6", "Discipline1, Discipline2, Discipline3"));
            list.Add(new MainType("Attr7", "Discipline1"));
            list.Add(new MainType("Attr8", "Discipline1"));
            list.Add(new MainType("Attr9", "Discipline1"));
            list.Add(new MainType("Attr10", "Discipline1"));
            list.Add(new MainType("Attr11", "Discipline2"));
            list.Add(new MainType("Attr12", "Discipline2"));
            list.Add(new MainType("Attr13", "Discipline2"));
            list.Add(new MainType("Attr14", "Discipline2"));
            list.Add(new MainType("Attr15", "Discipline3"));
            list.Add(new MainType("Attr16", "Discipline3"));
            list.Add(new MainType("Attr17", "Discipline3"));
            list.Add(new MainType("Attr18", "Discipline4"));
            list.Add(new MainType("Attr19", "Discipline4"));
            list.Add(new MainType("Attr20", "Discipline4"));
            list.Add(new MainType("Attr21", "Discipline4"));
            list.Add(new MainType("Attr22", "Discipline5"));
            list.Add(new MainType("Attr23", "Discipline5"));
            list.Add(new MainType("Attr24", "Discipline5"));
            list.Add(new MainType("Attr25", "Discipline6"));
            list.Add(new MainType("Attr26", "Discipline6"));
            list.Add(new MainType("Attr27", "Discipline5, Discipline6"));
            list.Add(new MainType("Attr28", "Discipline5, Discipline6"));
            list.Add(new MainType("Attr29", "Discipline5, Discipline6"));
 
            m_oRadGridView.ItemsSource = list;
        }
    }
    public class MainType
    {
        public MainType(string name, string desc)
        {
            DisciplineDescription = desc;
            Name = name;
        }
         
        public string DisciplineDescription { get; set; }
        public string Name { get; set; }
    }
}
 
 
 
 
DisciplineFilterCtrl.xml
 
<UserControl
    x:Class="DummyProject.DisciplineFilterCtrl"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">  
    <Border
        x:Name="LayoutRoot"
        BorderThickness="1"
        BorderBrush="#FF8A929E"
        Padding="5"
        Background="#FFDFE2E5">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition
                    Height="Auto" />
                <RowDefinition
                    Height="Auto" />
                <RowDefinition
                    Height="Auto" />
            </Grid.RowDefinitions>
            <TextBlock
                Grid.Row="0"
                Margin="2"
                Text="Show Attributes of Discipline" />
            <ListBox
                x:Name="m_oDisciplines"
                Grid.Row="1">               
            </ListBox>
            <StackPanel Orientation="Horizontal"
                        Grid.Row="2">
                <telerik:RadButton
                    Name="m_oFilterButton"
                    Content="Filter"
                    Click="m_oFilterButton_Click"
                    Margin="2"
                    Width="80" />
                <telerik:RadButton
                    Name="m_oClearButton"
                    Content="Clear"
                    Click="m_oClearButton_Click"
                    Margin="2"
                    Width="80" />
            </StackPanel>
        </Grid>
    </Border>
</UserControl>
 
 
 
DisciplineFilterCtrl.xml.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.GridView;
using System.Collections.ObjectModel;
using Telerik.Windows.Data;
 
namespace DummyProject
{
    public partial class DisciplineFilterCtrl : UserControl, IFilteringControl
 
    {
 
        ObservableCollection<Discipline> list = new ObservableCollection<Discipline>();
        public DisciplineFilterCtrl()
        {
            InitializeComponent();
             
            list.Add(new Discipline("Discipline1"));
            list.Add(new Discipline("Discipline2"));
            list.Add(new Discipline("Discipline3"));
            list.Add(new Discipline("Discipline4"));
            list.Add(new Discipline("Discipline5"));
            list.Add(new Discipline("Discipline6"));
            CreateListBoxWithCheckBox(list);
 
        }
        private void CreateListBoxWithCheckBox(ObservableCollection<Discipline> list)
        {
 
            List<ListBoxItem> listBoxItem = new List<ListBoxItem>();
            foreach (Discipline item in list)
            {
                 
                    ListBoxItem tempItem = new ListBoxItem();
                    CheckBox m_oCheckBox = new CheckBox();
                    m_oCheckBox.Checked += m_oCheckBox_Checked;
                    m_oCheckBox.Unchecked += m_oCheckBox_Unchecked;
                    m_oCheckBox.Content = item.Name;
                    m_oCheckBox.Tag = item;
                    tempItem.Content = m_oCheckBox;
                    listBoxItem.Add(tempItem);
                 
            }
            m_oDisciplines.ItemsSource = listBoxItem;
 
        }
 
        void m_oCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            FilterRecord();
            this.IsActive = true;
        }
 
        void m_oCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            FilterRecord();
            this.IsActive = true;
        }
        private CompositeFilterDescriptor compositeFilter;
        private Telerik.Windows.Controls.GridViewBoundColumnBase column;
        private Telerik.Windows.Data.FilterDescriptor[] DisciplineFilter;       
 
        
        public bool IsActive
        {
            get { return (bool)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }
        public static readonly DependencyProperty IsActiveProperty =
            DependencyProperty.Register(
                "IsActive",
                typeof(bool),
                typeof(DisciplineFilterCtrl),
                new System.Windows.PropertyMetadata(false));
 
 
        private void CreateFilters()
        {
            compositeFilter = new CompositeFilterDescriptor();
            compositeFilter.LogicalOperator = FilterCompositionLogicalOperator.Or;
            int count = list.Count;
            string dataMember = this.column.DataMemberBinding.Path.Path;
             
            DisciplineFilter = new FilterDescriptor[count];
            for (int i = 0; i < count; i++)
            {
                this.DisciplineFilter[i] = new Telerik.Windows.Data.FilterDescriptor(dataMember
                         , Telerik.Windows.Data.FilterOperator.Contains
                         , null);
            }
        }
 
        public void Prepare(Telerik.Windows.Controls.GridViewBoundColumnBase column)
        {
            this.column = column as Telerik.Windows.Controls.GridViewBoundColumnBase;
            if (this.column == null)
            {
                return;
            }
            if (this.compositeFilter == null)
            {
 
                CreateFilters();
            }
        }
 
        private void FilterRecord()
        {
            this.compositeFilter.FilterDescriptors.Clear();
            this.column.DataControl.FilterDescriptors.Clear();
            int i = 0;
            List<ListBoxItem> listItems = this.m_oDisciplines.ItemsSource as List<ListBoxItem>;
            if (null != listItems)
            {
                foreach (var item in listItems)
                {
                    CheckBox m_oCheckBox = item.Content as CheckBox;
                    if (null != m_oCheckBox && m_oCheckBox.IsChecked.Value)
                    {
 
                        this.compositeFilter.FilterDescriptors.Add(DisciplineFilter[i]);
                        this.DisciplineFilter[i].Value = m_oCheckBox.Content;
                        this.column.DataControl.FilterDescriptors.Add(DisciplineFilter[i]);
                        i++;
                    }
                }
            }
        }
 
        private void ClearFilter(object sender, RoutedEventArgs e)
        {
            this.compositeFilter.FilterDescriptors.Clear();
            this.column.DataControl.FilterDescriptors.Clear();           
        }
 
        private void m_oFilterButton_Click(object sender, RoutedEventArgs e)
        {
            FilterRecord();
            this.IsActive = true;
        }
 
        private void m_oClearButton_Click(object sender, RoutedEventArgs e)
        {
            List<ListBoxItem> listItems = this.m_oDisciplines.ItemsSource as List<ListBoxItem>;
            if (null != listItems)
            {
                foreach (var item in listItems)
                {
                    (item.Content as CheckBox).IsChecked = false;
                }
            }
            this.IsActive = false;
        }
         
         
    }
    public class Discipline
    {
        public Discipline(string name)
        {
            this.Name = name;
        }
        public string Name { get; set; }
    }
}
0
Rossen Hristov
Telerik team
answered on 21 Mar 2012, 04:56 PM
Hello,

You have to open a support ticket in order to be able to attach files.

In case your organization has purchased developer licenses, please contact the license holder and ask him to add you as licensed developer to his account. In this way you would be entitled to using our support system and receive guaranteed replies according to your license. You will be able to get access to our support services, the latest hotfixes and downloads from within your account. 

Once we update our records we would be able to provide you with adequate support services and answer your questions. 

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 >>
Tags
GridView
Asked by
Snype
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Snype
Top achievements
Rank 1
Share this question
or