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

Grouping of a column with a converter

10 Answers 155 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 03 Jul 2009, 12:13 PM
Hello,
I encountered a problem with the GridView WPF-Control. When I use a Converter in the DataMemberBinding of a column and group by that column, the result will be grouped by the unconverted value.

To show the problem, i set up a small test project with a small "Person" class with "Name" (string), "Age" (int) and "Status" (int) properties.
In the test project, three persons-objects will be created and bound to a GridView. Each property in a seperate column.
The "Status" column uses a converter for the DataMemberBinding:
<Window x:Class="RadGridViewColumnTest.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
    xmlns:System="clr-namespace:System;assembly=mscorlib"         
    xmlns:local="clr-namespace:RadGridViewColumnTest" 
    Title="Window1" Height="300" Width="300"
    <Grid> 
        <Grid.Resources> 
            <local:StatusConverter x:Key="StatusConverterResource"
            </local:StatusConverter> 
        </Grid.Resources> 
        <telerik:RadGridView x:Name="grid" AutoGenerateColumns="False" Grouping="grid_Grouping"
            <telerik:RadGridView.Columns> 
                <telerik:GridViewDataColumn HeaderText="Person" DataMemberBinding="{Binding .Name}" DataType="{x:Type System:String}"
                </telerik:GridViewDataColumn> 
                <telerik:GridViewDataColumn HeaderText="Age" DataMemberBinding="{Binding .Age}" DataType="{x:Type System:Int32}"/> 
                <telerik:GridViewDataColumn HeaderText="Status" DataMemberBinding="{Binding  Path=Status, Converter={StaticResource StatusConverterResource}}" IsSortable="True" DataType="{x:Type System:Int32}" IsGroupable="True"/> 
            </telerik:RadGridView.Columns> 
        </telerik:RadGridView> 
    </Grid> 
</Window> 
 

public class StatusConverter : IValueConverter 
    { 
        #region IValueConverter Members 
 
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            if ((int)value < 256) 
            { 
                return "< 256"
            } 
            else 
                return ">= 256"
 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
        #endregion 
    } 

The Status of the three persons are 122, 123, 321, and they appear correctly converted in the list.
Obviously the result, after a group by Status, should show the persons with Status 123 and 123 in one group because their Status is below 256.
But it seems the converter won't be used for grouping and every person appear in a single group.
Is there a solution for this problem ?

Thanks,  Michael

10 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 03 Jul 2009, 12:53 PM
Hello Michael,

You can achieve that if you add another string property to the Person class, for example:

        public string StatusRange
        {
            get { return this.status < 256 ? "< 256" : ">= 256"; }
        }

... and bind a column to this property instead of the "raw" integer Status.

Please, let me know if this does not help or if your architecture will not allow it.

Best wishes,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Michael
Top achievements
Rank 1
answered on 03 Jul 2009, 01:29 PM
Hello Ross,
Unfortunately this solution will only work for the test project. In the background use case, we work with third-party components and business object classes which should not be manipulated; so I hope it's possible to use the converter.
I noticed that, when i subscribed to the "Grouping"-Event of the grid, the event-args include a RadGroupDescription. The source property ("Status") is set in that GroupDescription but the link to the Converter of that GroupDescription is always null.
I think that's the problem why the grouping does not work as expected. Maybe it's possible to fix that case ?

Thanks, Michael
0
Rossen Hristov
Telerik team
answered on 08 Jul 2009, 12:32 PM
Hi Michael,

Unfortunately, RadGridView does not support grouping by a column bound with a converter. The only way to achieve this would be by modifying the business object directly, inheriting from it and adding the new property, or wrapping it in a wrapper class. Also, you may try with an ICustomTypeDescriptor and generate types dynamically.

Sincerely yours,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Michael
Top achievements
Rank 1
answered on 09 Jul 2009, 01:49 PM

Hello Ross,

it seems that the filter functionality doesn’t support binding with a converter as well. Is that right?

I don’t understand why a converter for a binding is supported for displaying the bounded data but not for grouping or filtering them.

Is this feature planned for one of the next releases?

Kind regards,

Michael

0
Vlad
Telerik team
answered on 09 Jul 2009, 02:13 PM
Hi Michael,

Generally IValueConverter is used only for display purposes. For all data operations like grouping, sorting, filtering, etc. the grid will work directly with ItemsSource. Currently the grid will apply all these operations directly on your data-base server if you bind the RadGridView to any LINQ provider like LINQ to SQL, ADO.NET Entities, Open Access, etc. however this will completely impossible if we try use value comverters.

Greetings,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Michael
Top achievements
Rank 1
answered on 04 Aug 2009, 06:28 AM

Hi Ross,

 

thanks for the hint with the ICustomTypeDescriptor. I think this is the perfect solution for me. I downloaded the Source from the Thread “Aggregate functions do not work for ICustomTypeDescriptor“ to get started with ICustomTypeDescriptor.

This works fine, but since .Net 2.0 there is the possibility to use a TypeDescriptionProvider. With this provider modifying the business object should be unnecessary. Until now I am not able to use this class for generating types dynamically. I am able to display the content of the dynamically generated properties but filtering or grouping throws exceptions.

Do you have some sample source or some hints for using this class with the RadGridView?

 

Thanks,

 

Michael

0
Stefan Dobrev
Telerik team
answered on 06 Aug 2009, 01:16 PM
Hello Michael,

I'm sending you an example that illustrates ICustomTypeDescriptor and TypeDescriptroProvider support. Hope you will find it useful.

All the best,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Michael
Top achievements
Rank 1
answered on 06 Aug 2009, 03:54 PM
Hello Stefan,

thanks for the example!
But I have one question: I thought that, when using "TypeDescriptionProvider", the business object doesn't have to inherit "CustomTypeDescriptor". So that the original business object can remain untouched.
So I added a new class for the TypeDescriptor and removed the base class CustomTypeDescriptor from the business object. Now I have a class that describes the additional properties of the original business object and the business object himself.
But when I do that with your code I have the same problem: The grid is viewing the data but if I try to group or filter the data the grid throws an exception.

Please help me...

Unfortunally it seems that I can not upload my VS-Solution to show you my problem. I think that would make it easier to understand what I mean, so I uploaded the file to: http://download.innovista.info/142922_typedescriptorsupport2.zip


Kind regards
Michael
0
Stefan Dobrev
Telerik team
answered on 07 Aug 2009, 12:52 PM
Hi Michael,

You are right. We currently require that your business object implements ICustomTypeDescriptor, even thought you have custom TypeDescriptorProvider. We will do our best to eliminate this requirement for our next major release.

Best wishes,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Michael
Top achievements
Rank 1
answered on 14 Aug 2009, 12:53 PM
Hi Stefan,

thanks for your help!
It would be great, if this problem will be solved in the next major release. Until then I will try to hack around this problem :-)

Kind regards,
Michael
Tags
GridView
Asked by
Michael
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Michael
Top achievements
Rank 1
Vlad
Telerik team
Stefan Dobrev
Telerik team
Share this question
or