Ticket T1285226
Visible to All Users

How to control sort order of Groups within a DxComboBox

created 10 days ago (modified 10 days ago)

I have a <DxComboBox> component with the GroupFieldName property populated.

Clipboard-File-1.png

Clipboard-File-2.png

Currently the groups are sorted alphabetically in ascending order. Is there a way I can change that to be descending, or even specify some arbitrary order I want the groups to appear in?

Comments (1)
GP GP
Gian Pascual (DevExpress Support) 9 days ago

    Hi,

    Thank you for contacting us. We need more time to analyze this use case. We'll get back to you once we have more information.

    Regards,
    Gian

    Answers approved by DevExpress Support

    created 5 days ago

    Hi,

    Thank you for your patience.

    Could you please clarify whether the group order needs to be changed dynamically in the UI, or it would be sufficient to modify it directly in code?

    In the latter case, we have a workaround that achieves the desired result. However, please note that it is not a straightforward solution. The idea is to declare a separate property to hold the numeric sort order value and use that for grouping. Then, in the GroupHeaderDisplayTemplate, you can display the actual group label you want.

    Razor
    <DxComboBox Data="Data" @bind-Value="Value" ValueFieldName="@nameof(ZipTieColorOptionDto.Color)" TextFieldName="@nameof(ZipTieColorOptionDto.Color)" GroupFieldName="@nameof(ZipTieColorOptionDto.StatusOrder)"> <GroupHeaderDisplayTemplate> <span>@indexToGroupDict[Convert.ToInt32(context.DisplayText)]</span> </GroupHeaderDisplayTemplate> </DxComboBox>
    C#
    List<ZipTieColorOption> ColorList { get; set; } List<ZipTieColorOptionDto> Data { get; set; } Dictionary<int, string> indexToGroupDict; string Value { get; set; } protected override void OnInitialized() { base.OnInitialized(); ColorList = new() { new(){Id = 1, Color="Green", Status = "A"}, new(){Id = 2, Color="Gray", Status = "A"}, new(){Id = 3, Color="Red", Status = "B"}, new(){Id = 4, Color="Blue", Status = "B"}, new(){Id = 5, Color="Yellow", Status = "B"}, new(){Id = 6, Color = "Plum", Status = "C" }, }; var statusesInReverse = ColorList.Select(c => c.Status).Distinct().Reverse() .Select((c, index) => new { Status = c, index }); var groupToIndexDict = statusesInReverse.ToDictionary(x => x.Status, x => x.index); indexToGroupDict = statusesInReverse.ToDictionary(x => x.index, x => x.Status); Data = ColorList.Select(c => new ZipTieColorOptionDto() { Id = c.Id, Color = c.Color, Status = c.Status, StatusOrder = groupToIndexDict[c.Status] }).ToList(); } public class ZipTieColorOption { public int Id { get; set; } public string Color { get; set; } public string Status { get; set; } } public class ZipTieColorOptionDto { public int Id { get; set; } public string Color { get; set; } public string Status { get; set; } public int StatusOrder { get; set; } }

    I attached a sample project for your reference.

    If this approach does not meet your needs, please share more details about your usage scenario. This information will put us in a better position to offer you a more accurate recommendation.

    We look forward to your response.

    Regards,
    Gian

      Comments (2)

        I was aiming to sort the groups in code, so the workaround should be sufficient. Thanks!

        GP GP
        Gian Pascual (DevExpress Support) 4 days ago

          Hi,

          I am happy to hear that the solution meets your requirements.

          If you need further assistance, feel free to contact us.

          Regards,
          Gian

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.