Filtering a column where DataMemberBinding uses a converter

2 posts, 0 answers
  1. Barnes
    Barnes avatar
    17 posts
    Member since:
    May 2012

    Posted 09 Jan 2013 Link to this post

    I have a situation where filtering is not working correctly. I have a gridview where columns are generated at runtime. This is done so we can decorate our classes with custom attributes to specify column headers, data formats etc. and can generate a grid that displays in the appropriate way based on the attributes of the given class.

    In one specific scenario there is an int property that need to be displayed as a timespan. The transformation from int to timespan is done using a ValueConverter. the data displays fine. I don't want to have to add an extra property to the class in question that returns a timespan conversion of the int in question.

    When filtering, the list of distinct values displayed matches those values displayed in the grid but the values that the user enters to filter the grid have to match the raw data. Very confusing! I am aware that the filtering engine only works on raw data when using formatting, but should this the case when using a converter?

    Is there a simple way to change the filter expression used for an individual column? I have seen a couple of forum posts but they seem to be excessively complex and not quite what I want. I would rather not create a new filtering control for a specific column.

    Thanks in advance.
  2. Rossen Hristov
    Rossen Hristov avatar
    2477 posts

    Posted 09 Jan 2013 Link to this post


    We are using LINQ for all data operations. Please, take a look at this article.

    Here is the important part.


    Distinct Values

    The filtering control that the user is presented with has two parts. The upper part of the control displays distinct values found in the respective column. The values that you see at any moment are the ones that are left after all other column filters have been applied. In other words, these values are collected from the visible rows only. This behavior can easily be configured as described in this article.

    The string representations that are displayed to the user are produced by taking the raw distinct value from the source collection and then applying the column’s DataMemberBinding IValueConverter and DataFormatString if any. For example, if you have specified a currency DataFormatString for your column, the user will see a nicely formatted $ 2.22 in the distinct values list. However, underneath this nicely formatted string the original Float value will be preserved and will be used for performing the actual filtering inside the data engine. One of the most common pitfalls is thinking that an IValueConverter defined on the DataMemberBinding of the column will affect the data operations in any way. It will not. The DataMemberBinding IValueConverter and the DataFormatString of the column are used for UI purposes only. They can only change the appearance of data into something more user-friendly such as $ 2.22, but they do not play any role in the data engine. Filtering is always performed with raw data values.

    Field Filters

    The lower part of the control represents the two field filters. These are two filtering criteria joined by a logical operator. The field filters allow the user to create filtering criteria like “age is greater than 18 and is less than 60”. Each field filters consists of two parts. The upper part is a combo box that allows the user to specify the type of comparison to be made, i.e. Is Equal To, Is Less Than, and so on. These are called filter operators. The available filter operators depend on the data type of the column. For example, for string columns you will see string-specific operators such as Contains and Is Contained In, whereas for numeric and date columns you will see the comparison operators Is Greater Than, Is Less Than, etc. Hereyou can find a table describing the available filter operators based on the column data type. If you want to remove some of the available filter operators that the user is presented with or change the operator that is selected by default you can use the FilterOperatorsLoading event of RadGridView as described here.

    The lower part of the field filter is called the field filter editor. Again, the editor that the user will see depends on the column data type. For example, if you have a date column the user will be able to enter the filtering criteria through a date time picker control. Depending on the column data type the most suitable editor is selected and used. Of course, you can always plug-in your very own field filter editor as described in this article. If you want to modify the appearance or behavior of the stock field filter editor that we have selected for you, you can easily do that by attaching to the FieldFilterEditorCreated event of RadGridView as demonstrated here.

    In short, since we are using LINQ for sorting, filtering and grouping (which very often goes all the way to SQL Server for example) those data operations can only be performed on the actual raw data -- not its UI representation.

    Now onto your case.

    You can develop a custom field filter editor (i.e. TextBox for example) that is smart enough to accept what the user enters and then convert it to the raw data  value before it is sent via the LINQ Where query. Developing a custom field filter editor is described in this article.

    I have attached a sample project that does something similar to this: it accepts percentage values like "30%" from the user and converts them to the correct double which is 0.30.

    Please, examine, run and debug the sample project. It should get you started.

    I hope this helps.

    All the best,

    Rossen Hristov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top