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

Default FilteringControl clears Guids from field

1 Answer 89 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.
Benjamin
Top achievements
Rank 1
Benjamin asked on 01 Sep 2011, 05:29 PM
I'm experiencing a problem with the default Filtering Control on the RadGridView when the type of the column is Guid.  I have found that the value typed into the text box is cleared as soon as the focus moves away from it.  This makes it impossible to search by Guid. Is this a known problem?  Is there a work around (other than creating my own filtering control)?

Thanks

1 Answer, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 06 Sep 2011, 01:34 PM
Hello Benjamin,

When you type in the filter text box, the filtering control is trying to compare strings. If the actual type of the property is a GUID, then it cannot be compared to a string, because GUIDs and string don't compare to each other out-of-the-box. 

We have introduced a new method to GridViewBoundColumnBase with one of our latest builds:

Copy Code
public virtual FrameworkElement CreateFieldFilterEditor()
{
    return FilterEditorFactory.CreateEditor(this.DataType);
}

So, I believe that this gives a perfect opportunity to replace the default field filter editor with the appropriate one for your case. This new editor can use the same IValueConverter as the column(if the column uses one) in its respective Binding.
public class MyGuidColumn : GridViewBoundColumnBase
   {
       public override FrameworkElement CreateFieldFilterEditor()
       {
           // Use the factory to produce an editor, but you can do it yourself if you want
           var textBox = (TextBox)FilterEditorFactory.CreateDefaultEditor();
 
           // "Value" is the name of the property on our ViewModel sitting behind the editor.
           var textBinding = new Binding("Value")
           {
               Mode = BindingMode.TwoWay,
               FallbackValue = null,
               Converter = new GuidConverter() // --> this the key point
           };
 
           // We will overwrite the default binding with a new one that uses the converter.
           textBox.SetBinding(TextBox.TextProperty, textBinding);
 
           return textBox;
       }
   }


You can use the factory to produce an editor if you like all predefined settings that you saw in the factory class, then once this editor comes out of the factory, we can take it and overwrite its Binding with a new one that uses the IValueConverter of the column.

I have prepared a small project with this approach. I am attaching the project with our binaries from August the 23rd that contain these changes. 

Basically, you can be in total control over what editors are shown and how are they bound and we will not make any changes to the grid. Thanks to this new virtual method that could be overridden. You can also use Factory if you like it, since it is all public now.

Another approach in order to be able to filter by typing in the text box, would be to change the type of your data before it is set to the RadGridView.  For example, you can add an additional read-only string property that will return the actual GUID.ToString(). Then filtering on this new string property will be fine. You will get string filtering out-of-the-box and you will not have to create any custom filtering control. 

I hope that one of these approaches is what you are looking for.

Greetings,
Didie
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Benjamin
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Share this question
or