The value "(YearRange IsEqualTo 2013)" is not of type "Telerik.Windows.Data.IFilterDescriptor

1 Answer 9 Views
GridView
Martin
Top achievements
Rank 1
Iron
Iron
Iron
Martin asked on 01 Jun 2021, 10:34 AM | edited on 01 Jun 2021, 11:38 AM

Hi.. 

I'm trying to implement a Custom Filter for a computed column in RadGridView.

The computed column is simply a Year-range eg: "2010-2015" - i want to filter all rows that contains a given year, eg: 2013.

Tthe computed column is using this type, where i've added the IEquitable interface. 

public class YearRange : IEquatable<int>
{
    public int FromYear { get; set; }

    public int ToYear { get; set; }

    public override string ToString()
    {
        return FromYear + "-" + ToYear;
    }

    public bool Equals(int other)
    {
        return FromYear >= other && other <= ToYear;
    }
}

The CustomGridFilter is implemented like this, where i've copied the telerik sample and modified it a bit. 

public partial class CustomGridFilter : UserControl, IFilteringControl
    {
        private GridViewBoundColumnBase column;
        private CompositeFilterDescriptor compositeFilter;
        private FilterDescriptor rangeFilter;

        #region IsActive DependencyProperty 
        public bool IsActive
        {
            get { return (bool)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }

        public static readonly DependencyProperty IsActiveProperty =
            DependencyProperty.Register(
                "IsActive",
                typeof(bool),
                typeof(CustomGridFilter),
                new PropertyMetadata(false));
        #endregion

        #region Aargang DependencyProperty 

        public int Aargang
        {
            get { return (int)GetValue(AargangProperty); }
            set { SetValue(AargangProperty, value); }
        }

        public static readonly DependencyProperty AargangProperty =
            DependencyProperty.Register(
                "Aargang",
                typeof(int),
                typeof(CustomGridFilter),
                new PropertyMetadata(0)
            );

        #endregion

        public CustomGridFilter()
        {
            InitializeComponent();

            DataContext = this;
        }

        public void Prepare(Telerik.Windows.Controls.GridViewColumn column)
        {
            this.column = column as GridViewBoundColumnBase;

            if (this.column == null)
            {
                return;
            }

            if (compositeFilter == null)
            {
                CreateFilters();
            }
        }

        private void CreateFilters()
        {
            string dataMember = column.DataMemberBinding.Path.Path;

            compositeFilter = new CompositeFilterDescriptor();

            rangeFilter = new FilterDescriptor(dataMember, FilterOperator.IsEqualTo, null);

            compositeFilter.FilterDescriptors.Add(rangeFilter);
        }

        private void OnFilter(object sender, RoutedEventArgs e)
        {
            rangeFilter.Value = Aargang;

            if (!column.DataControl.FilterDescriptors.Contains(compositeFilter))
            {
                column.DataControl.FilterDescriptors.Add(compositeFilter);
            }

            IsActive = true;

            var popup = this.ParentOfType<System.Windows.Controls.Primitives.Popup>();
            if (popup != null)
            {
                popup.IsOpen = false;
            }
        }

        private void OnClear(object sender, RoutedEventArgs e)
        {
            if (column.DataControl.FilterDescriptors.Contains(compositeFilter))
            {
                column.DataControl.FilterDescriptors.Remove(compositeFilter);
            }

            Aargang = 0;

            IsActive = false;

            var popup = this.ParentOfType<System.Windows.Controls.Primitives.Popup>();
            if (popup != null)
            {
                popup.IsOpen = false;
            }
        }
    }

The XAML is simply a textbox bound to the Aargang DependencyProperty which is functioning correctly considering the error message. 

My assumption is that IEquiatable<int> is supposed to execute the FilterOperator.IsEqualTo comparison ?

Why does it fail ?

EDIT: 

Debugging the "solution" from this question, https://www.telerik.com/forums/problem-with-custom-filter-with-custom-type - i've found that IEquatable is used for the GetDistinctValues operation while the ACTUAL comparison uses standard override Equals operator ... FFS... 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Martin
Top achievements
Rank 1
Iron
Iron
Iron
answered on 01 Jun 2021, 11:55 AM

IEquatable is misleading since its only used for finding Distinct elements for the Filter popup. 

FilterDescriptor only works with a type, matching the column type. 

YearRange changed to this (added Equals and GetHashCode)

public class YearRange : IEquatable<int>
{
    public int FromYear { get; set; }

    public int ToYear { get; set; }

    public int Year { get; set; }

    public override string ToString()
    {
        return FromYear + "-" + ToYear;
    }

    public override bool Equals(object obj)
    {
        var other = (YearRange)obj;

        return other.Year >= FromYear 
            && other.Year <= ToYear;
    }

    public override int GetHashCode()
    {
        return FromYear * ToYear;
    }

    public bool Equals(int other)
    {
        return FromYear >= other && other <= ToYear;
    }
}

OnFilter changed to this:

- note that the filter now receives a YearRange instead of an Integer. 
private void OnFilter(object sender, RoutedEventArgs e)
        {
            rangeFilter.Value = new YearRange { Year = Aargang };

            if (!column.DataControl.FilterDescriptors.Contains(compositeFilter))
            {
                column.DataControl.FilterDescriptors.Add(compositeFilter);
            }

            IsActive = true;

            var popup = this.ParentOfType<System.Windows.Controls.Primitives.Popup>();
            if (popup != null)
            {
                popup.IsOpen = false;
            }
        }

Tags
GridView
Asked by
Martin
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Martin
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or