A few threads have been posted over the years to ask about natural sorting, which is clicking a column header to go Ascending, Descending, then back to the default unsorted mode on the third click. I looked around for a tri-state or three-state toggle and didn't find any info. Very helpful but incomplete info is found here and here, so I thought I'd provide C# code to help anyone else looking to do this. Some threads refer to MasterGridViewTemplate.AllowNaturalSort but that property no longer exists. My code is adapted from the second forum posting referenced. It looks like the library has changed since Sean wrote his code.
Telerik.WinControls.Data.SortDescriptor.Direction is of type System.ComponentModel.ListDirection, and that enum only has two values. So the Boolean _sorted is used - if the grid is sorted in ascending or descending order, then use the sort order, but if we click past descending order, set _sorted to false (natural sort!) and clear the SortDescriptors.
Of course clearing SortDescriptors means this only works with single column sorting. Maybe someone will follow-up here with a solution that works with multiple columns.
HTH
using
System.ComponentModel;
bool
_sorted =
false
;
ListSortDirection _previousGridColumnSortDirection;
private
void
radGridView1_SortChanging(
object
sender, GridViewCollectionChangingEventArgs e)
{
try
{
Telerik.WinControls.Data.SortDescriptor sortField;
// Only proceed if we are changing an existing sorting (not starting a new one)
// Make sure we have an item
if
(e.NewItems !=
null
&& e.NewItems.Count > 0)
{
sortField = (Telerik.WinControls.Data.SortDescriptor)e.NewItems[0];
if
(e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.ItemChanging)
{
// If the current SortOrder is Ascending, and the previous SortOrder
// was Descending, then clear the sorting.
if
(_sorted)
{
if
(sortField.Direction == ListSortDirection.Ascending &&
_previousGridColumnSortDirection == ListSortDirection.Descending)
{
_sorted =
false
;
radGridView1.MasterGridViewTemplate.SortDescriptors.Clear();
}
//Save the SortOrder for the next iteration
_previousGridColumnSortDirection = sortField.Direction;
}
else
{
_sorted =
true
;
_previousGridColumnSortDirection = sortField.Direction;
}
}
if
(e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Add)
{
_sorted =
true
;
_previousGridColumnSortDirection = sortField.Direction;
}
}
}
catch
{
}
}