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

Sorting in DataGridTemplateColumn

1 Answer 372 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Ashiq asked on 05 Jul 2017, 03:01 PM

I have collection of student bind to RadDataGrid. The following code doesn't sort father name column, but it showing arrow icon in column header.


01.public class Student
03.  public int Id { get; set; }
04.  public string Name { get; set; }
05.  public Parent Parent { get; set; }
07.public class Parent
09.  public int Id { get; set; }
10.  public string FatherName { get; set; }
11.  public Parent FatherJob { get; set; }



01.<telerikGrid:RadDataGrid ItemsSource="{Binding Students}" AutoGenerateColumns="False">
02.    <telerikGrid:RadDataGrid.Columns>
03.        <telerikGrid:DataGridTemplateColumn CanUserSort="True" Header="Father">
04.            <telerikGrid:DataGridTemplateColumn.CellContentTemplate>
05.                <DataTemplate x:DataType="model:Student">
06.                    <TextBlock Text="{x:Bind Parent.FatherName}" />
07.                </DataTemplate>
08.            </telerikGrid:DataGridTemplateColumn.CellContentTemplate>
09.            <telerikGrid:DataGridTemplateColumn.SortDescriptor>
10.                <telerikCore:PropertySortDescriptor PropertyName="FatherName" />
11.            </telerikGrid:DataGridTemplateColumn.SortDescriptor>
12.        </telerikGrid:DataGridTemplateColumn>
13.    </telerikGrid:RadDataGrid.Columns>



1 Answer, 1 is accepted

Sort by
Lance | Senior Manager Technical Support
Telerik team
answered on 05 Jul 2017, 04:57 PM
Hello Ashiq,

Father is not a property of Student (only Parent is), therefore you cannot use a PropertySortDescriptor.

Instead, you can use a DelegateSortDescriptor (<-- click to see docs and example code). In your GeyKey() method, use to the FatherName property of the Parent object. 

public class CustomIKeyLookup : IKeyLookup
    public object GetKey(object instance)
        return (instance as Student).Parent.FatherName;

Here's the updated XAML:

<grid:RadDataGrid ItemsSource="{Binding Students}"
                <grid:DataGridTemplateColumn CanUserSort="True"
                        <DataTemplate x:DataType="local:Student">
                            <TextBlock Text="{x:Bind Parent.FatherName}" />
                                <customDescriptors:CustomIKeyLookup />

I've attached a runnable demo using your models and some sample data, the DataGrid properly sorts by FatherName.

If you have any further trouble, please open a Support Ticket here and share your problematic code with us.

Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Asked by
Top achievements
Rank 1
Answers by
Lance | Senior Manager Technical Support
Telerik team
Share this question