Hello,
Is there any way to control the sort order of records within a group? I'm setting the GroupByMode to FirstLetter on a column of names. I would expect the names within a group to be sorted alphabetically, but instead no sorting is applied at all. I've tried manually setting the SortComparer and GroupByComparer. The former never gets used, and the latter only compares the GroupBy records. It behaves this way in the feature browser examples, too.
Is this a bug, or is there something I'm missing? Anyone have a workaround other than manually sorting the backing collection?
Thanks,
Matusz
Hello Matusz,
Here is what I got with the example in the XamFeatureBrowser.
As you can see, the records are grouped by the first two characters. If you want to sort them (the default is ascending) you have to click the LabelPresenter in the group by area one more time, and the groups will be sorted in a descending order.
Yes, the Group By Records are sorted correctly. The problem is that the DataRecords within the groups are unsorted. Using that same sample, group by first letter and look at the records under D:
Despite the fact that CompanyName is sorted ascending, the records go Dr..., Du..., Di.... Changing the sort to descending does not change the order of these records.
Now if I change it to First 2 Characters, it will sort, because they all differ on the second character. So clearly it is applying the GroupByComparer to sort the data records within the groups. Is there any way I can force it to use a different comparison, i.e. full alphabetic ordering?
Any updates on this? My only progress has been through manually micro-managing the backing collection based on which field is grouped on, and this is an avenue I'd rather not continue down, since it's just duplicating functionality that's already built-in to the grid in other scenarios.
Hello,Sorry for the late response.
To the best of my knowledge this is not possible. The comparer which you will apply by selecting 1st, 2nd, 3rd character will sort the records by it and group them.
What I am thinking of is that you can create an unbound field, set the BindingPath to the field that you are grouping and make another sort on that column. The UnboundField will use the default alphabetical comparer to compare the items in the group, so you will have them sorted the way you want.
Please take a look at the screenshot.
You will see that they are now sorted inside the groups. I am using the ContactTitle field (after I copied the values from the CompanyName, which is the groupby field). Now they are Di, Dr,Du ascending sorted. Of course, you can hide the extra field (in your case the unbound one) so that it does not show up with the rest of the fields.
To anyone who's curious, after adding a hidden "SortCompanyName" field bound to the same property as CompanyName, here's how I handled the Sorting event. Note that you can't modify the existing sort description, you have to remove it and add a new one.
if (e.SortDescription.FieldName == "CompanyName"){
if (e.ReplaceExistingSortCriteria) { var removeMe = new List<FieldSortDescription>(); foreach (var sortedField in e.FieldLayout.SortedFields) { if (!sortedField.IsGroupBy) removeMe.Add(sortedField); } foreach (var description in removeMe) { e.FieldLayout.SortedFields.Remove(description); } } var sortDescription = e.FieldLayout.SortedFields.FirstOrDefault(x => x.FieldName == "SortCompanyName"); if (sortDescription != null) { e.FieldLayout.SortedFields.Remove(sortDescription); e.FieldLayout.SortedFields.Add(new FieldSortDescription("SortCompanyName", e.SortDescription.Direction, false)); } else { e.FieldLayout.SortedFields.Add( new FieldSortDescription("SortCompanyName", e.SortDescription.Direction, false)); }}
(Edit: Added the ReplaceExistingSortCriteria block to support sorting by multiple fields.)
Yes, you will have to handle the Sorting event and check the sorting direction of the grouped field, so that you can properly update the hidden one.
Thanks Alex, I think this will be the route I take.
This solution does present another issue, but I'm pretty sure I can work around it. The sort order will only work until the first time a user decides to sort by a different column, and then return to the original sort. I will have to monitor for any attempt by the user to sort by the grouped field, and manually add a sort description for the unbound field.