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()
{
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.