Search code examples
c#wpfsortingdatagridlistcollectionview

Removing the sort arrow from DataGrid programmatically in WPF


In my WPF application, I have a DataGrid with some columns that displays my sortable data. The user can sort and subsort whichever column he wants.

I added a button that should clear the sorting and return the DataGrid to its unsorted state using the MVVM pattern (meaning that the button is bound to a RelayCommand in the ViewModel, that clears the DataGrid's datasource's SortDescriptions.)

This is how the code looks now:

ViewModelLocator.MyViewModel.GroupedItems.SortDescriptions.Clear();

The DataGrid's DataSource is the GroupedItems object (of type ListCollectionView). When I click the button, I see that the DataGrid returns to its original, non-sorted state, however, the sorting arrows in the column headers remain as if the DataGrid is still sorted. How can I programmatically remove these arrows?


Solution

  • To remove arrows in DataGrid try:

    foreach (var column in dt.Columns)
    {
        column.SortDirection = null;
    }
    

    Where dt is DataGrid.