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

RadListControl - sort by value?

2 Answers 133 Views
ListControl
This is a migrated thread and some comments may be shown as answers.
Lou
Top achievements
Rank 1
Lou asked on 18 Mar 2015, 05:55 PM
Hi, is it possible to sort items in the RadListControl by item value instead of item text?

Thanks,

-Lou

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Mar 2015, 10:52 AM
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.

 
0
Lou
Top achievements
Rank 1
answered on 24 Mar 2015, 01:34 PM
Thanks for that information Dess - very helpful.

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