Problem with Grid Filter and Custom Columns

2 posts, 0 answers
  1. David
    David avatar
    12 posts
    Member since:
    Mar 2009

    Posted 04 Feb 2012 Link to this post

    I am using a RadGridView to display several columns with custom data types (Angle, Distance, Speed, etc.) I have almost everything working, the cells display the correct information, sorting works, and filtering seems to be working except when I type a value into the filter text box of the default filter control and hit return or press the 'Filter' button the text just goes away. Again, to be clear, I select the funnel, I can check individual items and that works. But if I want to show all entries whose Speed is less than 30 m/s for instance. I can select the Is less than option and type in a value of 30. When I hit Enter or press 'Filter' the text box goes blank and nothing happens. I have found how I can create an entirely custom filter control, but I would just as soon use the default (less work and better consistency with other columns).

    Any ideas?

    Dave Goughnour
  2. Rossen Hristov
    Rossen Hristov avatar
    2477 posts

    Posted 06 Feb 2012 Link to this post


    This behavior is expected. There is no automatic way to translate the string that the user types in into your custom type.

    One thing you can try to do is to specify a TypeConverter for your custom types, so that conversion to and from string would be possible.

    Another thing that you can do is create a custom filter editor and assign it to be used by the respective column. There is a virtual method called CreateFieldFilterEditor. You can override it and supply your custom editor. Your custom editor should "know" how to produce instances of your custom type. Here is an example of something similar -- the idea would be exactly the same:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using Telerik.Windows.Controls;
    using Telerik.Windows.Data;
    namespace Testing
        public class TimeSpanColumn : GridViewBoundColumnBase
            public override FrameworkElement CreateFieldFilterEditor()
                var editor = new TextBox();
                // "Value" is the name of the property on our underlying ViewModel.
                // You have to bind your editor to this property.
                var textBinding = new Binding("Value")
                    Mode = BindingMode.TwoWay,
                    FallbackValue = string.Empty,
                    Converter = new TimeSpanConverter()
                editor.SetBinding(TextBox.TextProperty, textBinding);
                // Remove these if you don't like them
                TextBoxBehavior.SetUpdateTextOnEnter(editor, true);
                TextBoxBehavior.SetSelectAllOnGotFocus(editor, true);
                return editor;
            private class TimeSpanConverter : IValueConverter
                public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                    // FilterDescriptor.UnsetValue means "the filter is not active"
                    if (value == FilterDescriptor.UnsetValue)
                        return string.Empty;
                    return value.ToString();
                public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
                    if (!string.IsNullOrEmpty((string)value))
                        var result = TimeSpan.Zero;
                        if (TimeSpan.TryParse((string)value, out result))
                            return result;
                    // FilterDescriptor.UnsetValue means "deactivate the filter"
                    return FilterDescriptor.UnsetValue;

    The idea is that the custom editor will take the string that the user types in, convert it to the custom Type (TimeSpan in my sample) and then assign it to the Value property of our view model.

    I hope this helps. Let me know if there are problems.
    All the best,
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top