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

Sorting in DataGridTemplateColumn

1 Answer 418 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ashiq
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
02.{
03.  public int Id { get; set; }
04.  public string Name { get; set; }
05.  public Parent Parent { get; set; }
06.}
07.public class Parent
08.{
09.  public int Id { get; set; }
10.  public string FatherName { get; set; }
11.  public Parent FatherJob { get; set; }
12.}

 

 

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>
14.</telerikGrid:RadDataGrid>

 

 

1 Answer, 1 is accepted

Sort by
0
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}"
                          AutoGenerateColumns="False">
            <grid:RadDataGrid.Columns>
                <grid:DataGridTemplateColumn CanUserSort="True"
                                             Header="Father">
                    <grid:DataGridTemplateColumn.CellContentTemplate>
                        <DataTemplate x:DataType="local:Student">
                            <TextBlock Text="{x:Bind Parent.FatherName}" />
                        </DataTemplate>
                    </grid:DataGridTemplateColumn.CellContentTemplate>
 
                    <grid:DataGridTemplateColumn.SortDescriptor>
                        <core:DelegateSortDescriptor>
                            <core:DelegateSortDescriptor.KeyLookup>
                                <customDescriptors:CustomIKeyLookup />
                            </core:DelegateSortDescriptor.KeyLookup>
                        </core:DelegateSortDescriptor>
                    </grid:DataGridTemplateColumn.SortDescriptor>
                </grid:DataGridTemplateColumn>
            </grid:RadDataGrid.Columns>
        </grid:RadDataGrid>

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.

Regards,
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
Tags
DataGrid
Asked by
Ashiq
Top achievements
Rank 1
Answers by
Lance | Senior Manager Technical Support
Telerik team
Share this question
or