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

Filtering a column based on TypeConverter results?

2 Answers 323 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 03 Apr 2017, 10:39 PM

Hello,

Let's suppose I have a large DB of vehicles. One of the tables is tblColors that has a numeric ColorCode and a string ColorName (say there's about a hundred of those).

The main table contains a ColorCode of the vehicle but I need to show ColorName in my grid. What I do is I create a TypeConverter that goes like

public class ColorConverter
{
  public object Convert(object, Type, object, CultureInfo)
  {
    byte? colorCode = value as byte?;
    swtich (colorCode)
    {
      case 1: return "White";
                  break;
      .....
    }
........
}

 

Then I go to my grid and create a column:

...
<Grid.Resources>
  <r:ColorConverter x:Key="ColorConverter" />
</Grid.Resources>
...
 
<telerik:RadGridView Name="myGrid" AutoGenerateColumns="false" ... >
  <telerik:RadGridView.Columns>
    ...
    <telerik:GridViewDataColumn Header="Color" DataMemberBinding="{Binding ColorCode, Converter={StaticResource ColorConverter}}" />
    ...
  </telerik:RadGridViewColumns>
  ...
</telerik:RadGridView>

 

First of all: am I doing this right? I don't want to query my other table on each row loading so I just preload (or hard-code) the code->name relation.

Second question is, now when I click on the filtering symbol for the Color column, I'm getting the numbers not the converted values. I could preload the values (as a List<String>) in the DistinctValuesLoading event but then when I'm trying to filter I'm getting a "System.FormatException: Input string was not in a correct format."

Thank you.

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Yoan
Telerik team
answered on 06 Apr 2017, 01:01 PM
Hi Alex,

I am afraid that IValueConverters are only used for presentation purposes. They play absolutely no part in the data engine. Filtering is always performed with the raw data values. I can suggest reading the Applied IValueConverter on the DataMemberBinding help article for a reference. You can create a calculated property on your model which will return the correct color name. Then you can set this property as FilterMemberPath of the column.

I hope this helps.

Regards,
Yoan
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Alex
Top achievements
Rank 1
answered on 07 Apr 2017, 03:49 PM

Thank you, Yoan.

I see now that I'm not using the Value Converter for what it's supposed to be used.

Tags
GridView
Asked by
Alex
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Alex
Top achievements
Rank 1
Share this question
or