Sort one column with custom comparer

2 posts, 0 answers
  1. Tinus
    Tinus avatar
    26 posts
    Member since:
    Jun 2012

    Posted 09 Aug 2016 Link to this post


    I'm trying to get one column sorted in DetailsView with a custom comparer. The other columns are fine with the default comparer.

    How can I attach my custom comparer to the correct column?

  2. Hristo
    Hristo avatar
    1522 posts

    Posted 12 Aug 2016 Link to this post

    Hello Martin,

    Thank you for writing.

    The custom comparer is set on the DataView object and not per column. The example below shows how you can create a custom comparer and sort a particular column according to the values of another column: 
    public partial class Form1 : Form
        public Form1()
            new RadControlSpyForm().Show();
            this.radListView1.Dock = DockStyle.Fill;
            this.radListView1.ViewType = ListViewType.DetailsView;
            this.radListView1.DataSource = this.GetData();
            this.radListView1.DisplayMember = "Name";
            this.radListView1.ValueMember = "ID";
            this.radListView1.EnableSorting = true;
            this.radListView1.EnableColumnSort = true;
        private BindingList<Person> GetData()
            BindingList<Person> dataSource = new BindingList<Person>()
                new Person(1, "Nancy Davolio","507 - 20th Ave. E.Apt. 2A", new DateTime(1948, 12,8)),
                new Person(2, "Andrew Fuller","908 W. Capital Way"new DateTime(1952, 2, 19)),
                new Person(3, "Janet Leverling","722 Moss Bay Blvd."new DateTime(1963, 8, 30)),
                new Person(4, "Margaret Peacock","110 Old Redmond Rd."new DateTime(1937, 9, 19))
            return dataSource;
        private void radButton1_Click(object sender, EventArgs e)
            SortDescriptor sort = new SortDescriptor("Name", ListSortDirection.Ascending);
            this.radListView1.ListViewElement.DataView.Comparer = new ListViewCustomComparer(this.radListView1.ListViewElement);
    public class ListViewCustomComparer : IComparer<ListViewDataItem>
        RadListViewElement listViewElement;
        public ListViewCustomComparer(RadListViewElement listViewElement)
            this.listViewElement = listViewElement;
        public int Compare(ListViewDataItem x, ListViewDataItem y)
            //Sort the Name column according to the Id column as descending
            int row1Id = (int)x["Id"];
            int row2Id = (int)y["Id"];
            if (row1Id > row2Id)
                return -1;
            else if (row1Id < row2Id)
                return 1;
                return 0;

    I hope this helps. Please let me know if you need further assistance.

    Hristo Merdjanov
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
Back to Top