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

Prevent sort being set to nothing

1 Answer 48 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 20 Feb 2018, 02:28 AM

I have a simple RadDataGrid with sorting set to... 

    UserSortMode="Single"

    ColumnDataOperationsMode="Inline".

 

Clicking a header for a column goes through three steps. Sort ascending, sort descending and then no sorting. I want to prevent sorting being removed and so clicking the header when it is at sort descending should set it to ascending again. Is this possible?

1 Answer, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 22 Feb 2018, 11:56 AM
Hello Phil,

There is no straight-forward way for removing the "no sorting" option, however, you could achieve it by creating a custom ColumnHeaderTapCommand and switching the SortDescriptor in its Execute method. Here is the implementation:

- DataGrid definition:

<grid:RadDataGrid ColumnDataOperationsMode="Inline" x:Name="grid" Grid.Row="2" ItemsSource="{Binding}" UserSortMode="Single">
    <grid:RadDataGrid.SortDescriptors>
        <grid1:PropertySortDescriptor PropertyName="Name"/>
    </grid:RadDataGrid.SortDescriptors>
</grid:RadDataGrid>

- custom ColumnHeaderTapCommand:

public class CustomColumhHeaderTapCommand : DataGridCommand
{
    public CustomColumhHeaderTapCommand()
    {
        this.Id = CommandId.ColumnHeaderTap;
    }
 
    public override bool CanExecute(object parameter)
    {
        return true;
    }
 
    public override void Execute(object parameter)
    {
        var propertyDescriptor = this.Owner.SortDescriptors.FirstOrDefault(a => a is PropertySortDescriptor && ((PropertySortDescriptor)a).PropertyName == "Name");
        if (propertyDescriptor != null)
        {
            this.Owner.SortDescriptors.Remove(propertyDescriptor);
            if (propertyDescriptor.SortOrder == SortOrder.Ascending)
            {
                propertyDescriptor.SortOrder = SortOrder.Descending;
                this.Owner.SortDescriptors.Add(propertyDescriptor);
                ((ColumnHeaderTapContext)parameter).Column.SortDirection = SortDirection.Descending;
            }
            else
            {
                propertyDescriptor.SortOrder = SortOrder.Ascending;
                this.Owner.SortDescriptors.Add(propertyDescriptor);
                ((ColumnHeaderTapContext)parameter).Column.SortDirection = SortDirection.Ascending;
            }
        }
    }
}


- Adding the Command:

this.grid.Commands.Add(new CustomColumhHeaderTapCommand());

Have in mind that this is a custom implementation and I cannot guarantee that it will work in all scenarios.
Try it and let me know whether this would work for you.

Regards,
Yana
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
Phil
Top achievements
Rank 1
Answers by
Yana
Telerik team
Share this question
or