RadListControl - sort by value?

3 posts, 0 answers
  1. Lou
    Lou avatar
    61 posts
    Member since:
    Aug 2014

    Posted 18 Mar 2015 Link to this post

    Hi, is it possible to sort items in the RadListControl by item value instead of item text?

    Thanks,

    -Lou
  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 23 Mar 2015 Link to this post

    Hello Lou,

    Thank you for writing.

    Sorting in RadListControl is controlled only via the SortStyle property. It supports ascending sort style, descending sort style and no sort style. Setting sorting through code looks like this:
    this.radListControl1.SortStyle = Telerik.WinControls.Enumerations.SortStyle.Ascending;

    So, if you want to use custom comparer method for sorting operation you should use this approach:
    public Form1()
    {
        InitializeComponent();
        List<Item> items = new List<Item>();
        for (int i = 0; i < 20; i++)
        {
            items.Add(new Item(i, i + ".Item"));
        }
     
        this.radListControl1.DataSource = items;
        this.radListControl1.DisplayMember = "Name";
        this.radListControl1.ValueMember = "Id";
     
        List<RadListDataItem> sortList = new List<RadListDataItem>(radListControl1.Items);
        sortList.Sort(new MyComparer());
        radListControl1.Items.Clear();
        radListControl1.Items.AddRange(sortList);
    }
     
    public class MyComparer : IComparer <RadListDataItem>
    {
        public int Compare(RadListDataItem x, RadListDataItem y)
        {
            Item xItem = x.DataBoundItem as Item;
            Item yItem = y.DataBoundItem as Item;
     
            return xItem.Id.CompareTo(yItem.Id);
        }
    }
     
    public class Item
    {
        public int Id { get; set; }
     
        public string Name { get; set; }
     
        public Item()
        {
        }
     
        public Item(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }

    I hope this information helps. Should you have further questions, I would be glad to help.
     
    Regards,
    Dess
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Lou
    Lou avatar
    61 posts
    Member since:
    Aug 2014

    Posted 24 Mar 2015 in reply to Dess Link to this post

    Thanks for that information Dess - very helpful.

    -Lou
Back to Top