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

Custom sorting in DetailView?

3 Answers 178 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 10 Mar 2012, 12:44 AM
I understand you can perform custom sorting using a grid and a tree but how do you do this from within a RadListView?  Basically I have a bound column that contains strings, some are words, some are numbers.  I need to sort them in a "unique" manner.  Can I pass a column a custom sort method or hook a sort event or pass a custom sort descriptor or ???

3 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Todorov
Telerik team
answered on 13 Mar 2012, 10:47 AM
Hi Dan,

Thank you for contacting us.

RadListView is built over the same data layer as RadTreeView and RadGridView. Therefore, it is possible to achieve custom sorting by creating a custom comparer. The following code snippet demonstrates this:
public Form1()
{
    InitializeComponent();
    this.radListView1.ViewType = ListViewType.DetailsView;
}
 
private void radButton1_Click(object sender, EventArgs e)
{
    this.radListView1.Columns.Add("Number");
    this.radListView1.Columns.Add("Freight");
    Random rand = new Random();
    for (int i = 0; i < 30; i++)
    {
        ListViewDataItem item = new ListViewDataItem();
        this.radListView1.Items.Add(item);
        item[0] = i;
        item[1] = (decimal)(rand.NextDouble() * Int32.MaxValue);
    }
 
    this.radListView1.SortDescriptors.Add(new Telerik.WinControls.Data.SortDescriptor("Number", ListSortDirection.Ascending));
    this.radListView1.ListViewElement.DataView.Comparer = new ListViewCustomComparer(this.radListView1.ListViewElement);
    this.radListView1.EnableSorting = true;
}
 
 
public class ListViewCustomComparer : IComparer<ListViewDataItem>
{
    RadListViewElement listViewElement;
 
    public ListViewCustomComparer(RadListViewElement listViewElement)
    {
        this.listViewElement = listViewElement;
    }
 
    public int Compare(ListViewDataItem x, ListViewDataItem y)
    {
        decimal row1Freight = (decimal)x["Freight"];
        decimal row2Freight = (decimal)y["Freight"];
        if (row1Freight > row2Freight)
        {
            return 1;
        }
        else if (row1Freight < row2Freight)
        {
            return -1;
        }
        else
        {
            return 0;
        }
    }
}

Note that the entire sort operation is defined by this comparer. This means that you should take into account the contents of the SortDescriptors collection the RadListView when implementing this custom comparer (that is why I am passing a RadListViewElement as an argument to the constructor of the above comparer).

I hope you find this useful. Do not hesitate to write back if you have any further questions.

All the best,
Ivan Todorov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Dan
Top achievements
Rank 1
answered on 13 Mar 2012, 04:08 PM
Thank you Ivan for the complete example.  I'm impressed with the responses in the forum thus far.

I've used comparers before so just getting me to setting the right property was what was needed.

You indicated:

"Note that the entire sort operation is defined by this comparer. This means that you should take into account the contents of the SortDescriptors collection the RadListView when implementing this custom comparer (that is why I am passing a RadListViewElement as an argument to the constructor of the above comparer)."

I understand that the comparer class will be utilized for the entire sort operation.  Are you simply saying that I should also take into account any values (column properties that are set) in the SortDescriptors?  For example, you set your SortDescriptor to be associated with the "Numbers" column yet sorted on "Freight".  If I perform a custom sort, I should take the different columns being set in the SortDescriptors into account and especially in DetailView.  If that is the case, I get it.

Thanks again.
0
Ivan Todorov
Telerik team
answered on 16 Mar 2012, 10:30 AM
Hi Dan,

Yes, you have understood this correctly. Generally, when you have not set a custom comparer, the default comparer of RadDataView does exactly this: reads the contents of the SortDescriptors collection and sorts the items according to the read values.

When you create a custom comparer that compares for example only the values of the "Freight" column in ascending order, this means that the the values of the SortDescriptors collection won't affect in any manner the sort order. However, at least one SortDescriptor should be available in the SortDescriptors collection because otherwise no sorting is performed at all.

Should you have any additional questions, I will be glad to answer them.

Greetings,
Ivan Todorov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
ListView
Asked by
Dan
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Dan
Top achievements
Rank 1
Share this question
or