I want to sort my items via a custom sort order, similar to the implementation detailed here: http://docs.telerik.com/devtools/wpf/controls/radgridview/sorting/custom#custom-sorting-handling-sorting-event
However when I do it seems that e.OldSortingState is always equal to SortingState.None and so I don't get the functionality where first click on column = Ascending, second = Descending, and third = reset to default sorting (ie. None).
The example below should mostly set the sorting to the way it would be if I didn't handle the Sorting event myself. It's only for demonstration. My issue is that I can't ever get to the "else if" and "else" blocks.
private
void
MainDataGridOnSorting(
object
sender, GridViewSortingEventArgs e)
{
RadGridView radGridView = e.Source
as
RadGridView;
if
(radGridView ==
null
)
{
e.Cancel =
true
;
return
;
}
DataView dataView = radGridView.DataContext
as
DataView;
if
(dataView ==
null
)
{
e.Cancel =
true
;
return
;
}
IEnumerable<DataRowView> dataRows = dataView.Cast<DataRowView>();
//If the sorting state is none, sort the items ascending.
if
(e.OldSortingState == SortingState.None)
{
e.NewSortingState = SortingState.Ascending;
e.Column.SortingState = SortingState.Ascending;
dataRows =
dataRows
.OrderBy(row => row.Field<
string
>(e.Column.UniqueName));
}
//If the sorting state is none, sort the items descending.
else
if
(e.OldSortingState == SortingState.Ascending)
{
// Cannot reach this block
e.NewSortingState = SortingState.Descending;
e.Column.SortingState = SortingState.Descending;
dataRows =
dataRows.OrderByDescending(row => row.Field<
string
>(e.Column.UniqueName));
}
//If the sorting state is descending, apply default sorting to the items.
else
{
// Cannot reach this block
e.NewSortingState = SortingState.None;
e.Column.SortingState = SortingState.None;
dataRows =
dataRows.OrderBy(row => row.Field<
int
>(
"PrimaryRow"
));
}
radGridView.ItemsSource = dataRows;
e.Handled =
true
;
e.Cancel =
true
;
}