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
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:
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.