This is a migrated thread and some comments may be shown as answers.

Order by checked in the drop down

1 Answer 142 Views
CheckedListBox
This is a migrated thread and some comments may be shown as answers.
Frank
Top achievements
Rank 1
Frank asked on 21 Jan 2021, 08:52 PM
Is there a way that you can order by the selected items in the CheckedList box?  So that when the dropdown list opens all the checked items are at the top.

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 22 Jan 2021, 06:01 AM

Hello, Frank,

Since the product of this ticket is specified as RadCheckedListBox, but you mention several times drop down, it is not quite clear whether you are using a RadCheckedListBox or a RadCheckedDropDownList. Could you please clarify?

Similar to RadListView or any other control that lists the items in the view, RadCheckedListBox is intended to show its Items in the order they appear in the Items collection. However, you can benefit the custom sorting functionality to achieve the desired items order considering the check state: https://docs.telerik.com/devtools/winforms/controls/listview/features/sorting#custom-sorting 

I have prepared a sample code snippet for your reference:

        public RadForm1()
        {
            InitializeComponent();

            for (int i = 0; i < 20; i++)
            {
                ListViewDataItem item = new ListViewDataItem(); 
                item.Text = "Item" + i;
                this.radCheckedListBox1.Items.Add(item);
            }
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            radCheckedListBox1.EnableSorting = true;
            SortDescriptor sort = new SortDescriptor("Checked", ListSortDirection.Ascending);
            this.radCheckedListBox1.ListViewElement.DataView.Comparer = new ListViewCustomComparer(this.radCheckedListBox1.ListViewElement);
            radCheckedListBox1.SortDescriptors.Add(sort);
        }

        public class ListViewCustomComparer : IComparer<ListViewDataItem>
        {
            RadListViewElement listViewElement;

            public ListViewCustomComparer(RadListViewElement listViewElement)
            {
                this.listViewElement = listViewElement;
            }

            public int Compare(ListViewDataItem x, ListViewDataItem y)
            {
                ToggleState xToggle = x.CheckState;
                ToggleState yToggle = y.CheckState;
                if (xToggle == ToggleState.On)
                {
                    if (yToggle== ToggleState.On)
                    {
                        return x.Text.CompareTo(y.Text);
                    }
                    return 0;
                }
                else if (yToggle== ToggleState.On)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }

The attached gif file illustrates the achieved functionality.

In a similar way you can achieve identical behavior for RadCheckedDropDownList as well: https://docs.telerik.com/devtools/winforms/controls/dropdown-listcontrol-and-checkeddropdownlist/dropdownlist/features/sorting#customizing-sort-order 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
CheckedListBox
Asked by
Frank
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or