Sorting in DataGridTemplateColumn

2 posts, 0 answers
  1. Ashiq
    Ashiq avatar
    1 posts
    Member since:
    Aug 2015

    Posted 05 Jul 2017 Link to this post

    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>



  2. Lance | Tech Support Engineer, Sr.
    Lance | Tech Support Engineer, Sr. avatar
    561 posts

    Posted 05 Jul 2017 Link to this post

    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
Back to Top