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
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.
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.
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.