Hi,
I want to sort a ListView control in C# based on columns. I have created ColumnClickEventHandler:
private void contactsListView_ColumnClick(object sender, ColumnClickEventArgs e)
{
contactsListView.ListViewItemSorter = new ListViewItemComparer(e.Column);
contactsListView.Sort();
}
Where ListViewItemComparer class looks like this:
class ListViewItemComparer : IComparer<ListViewItem>
{
private int col;
public ListViewItemComparer()
{
col = 0;
}
public ListViewItemComparer(int column)
{
col = column;
}
public int Compare(ListViewItem x, ListViewItem y)
{
int returnVal = -1;
returnVal = String.Compare(x.SubItems[col].Text, y.SubItems[col].Text);
return returnVal;
}
}
This code won't compile cause contactsListView.ListViewItemSorter is type IComparer and class ListViewItemComparer is IComparer.
Is there a way to explicitly cast one to the other? Or just do sorting another way?
Thanks in advance