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

Set SortDirection on column but when data changes it is not automatically sorted

1 Answer 797 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 05 Dec 2018, 06:18 PM

I have a Data grid that looks like so:

<grid:RadDataGrid Grid.Row="1" ItemsSource="{Binding Notes}" AutoGenerateColumns="False" UserEditMode="None" UserFilterMode="Disabled" UserGroupMode="Disabled" SelectionMode="None">
    <grid:RadDataGrid.Columns>
        <grid:DataGridDateColumn Header="Time" PropertyName="Time" CellContentFormat="{}{0:g}" SizeMode="Auto" SortDirection="Descending"/>
        <grid:DataGridTextColumn Header="Note" PropertyName="Message" SizeMode="Stretch"/>
    </grid:RadDataGrid.Columns>
</grid:RadDataGrid>

 

The Time column shows the correct sort direction indicator but when the data changes (Notes is an ObservableCollection) it is not sorted automatically. Do I have to pre-sort the data I added to Notes?  This seems a bit redundant.

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 05 Dec 2018, 09:43 PM
Hello Rob,

Correct, the sorting doesn't get reapplied every time a new item is added to the original bound collection. This is because the bound data and the sorted data are not directly tied to each other. When the DataGrid sorts the data, this occurs in the DataView, not on the bound collection.

If you change the items source and want sorting done again, clear and re-add the descriptor:

MyDataGrid.SortDescriptors.Clear();
MyDataGrid.SortDescriptors.Add(new PropertySortDescriptor{PropertyName = "Notes"});

Alternatively, you could reset the ItemsSource, which will cause the sort to be reapplied once the data is added again. But this would cause a 'flashing' appearance.

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
Rob
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or