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

Sort one column with custom comparer

1 Answer 103 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Tinus
Top achievements
Rank 2
Tinus asked on 09 Aug 2016, 05:08 PM

Hi,

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?

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 12 Aug 2016, 01:50 PM
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 and sort a particular column according to the values of another column: 
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
 
        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);
        radListView1.SortDescriptors.Add(sort);
        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;
        }
        else
        {
            return 0;
        }
    }
}

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

Regards,
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.
Tags
ListView
Asked by
Tinus
Top achievements
Rank 2
Answers by
Hristo
Telerik team
Share this question
or