Introduction
My solution is an alternative to C# Sorted ListView, posted by Carlos H.Perez. The main difference is that I'm not trying to extend ListView control at all. Instead of that I suggest using a standard ListView
with customizable ListViewSorter
.
The main idea is to tie ListView
column headers with appropriate delegates, which can handle column cells comparison activities. So I declare ListViewSorter.Comparer
delegate, and enable to store column-delegate pairs into sorter.
public delegate int Comparer(string x, string y);
public Comparer this[ColumnHeader _column] {...}
The sorter itself is tied to a list control and handles the ColumnClick
events for that list. On each column the click sorter activates the clicked column.
protected void Column_Click(object sender, ColumnClickEventArgs e)
{
ListView _list = sender as ListView;
if (_list != this.list)
throw new Exception("The event sender does not match the list");
CleanUp();
ColumnHeader _column = this.list.Columns[e.Column];
SortBy(_column, this.column ! =_column ||
this.list.Sorting!=SortOrder.Ascending);
}
That active column later is used by sorting logic to choose correct comparer and to compare cells.
public void SortBy(ColumnHeader _column, bool asc)
{
if (this.list == null) throw new Exception("The list is null");
if (_column == null) throw new Exception("The column is null");
if (this[_column] == null) return;
this.column = _column;
this.list.Sorting = SortOrder.None;
this.list.Sorting = asc ? SortOrder.Ascending : SortOrder.Descending;
}
Bellow is cell comparison through the Comparer
implementation.
int IComparer.Compare(object x, object y)
{
if (this.list == null || this.list.Sorting == SortOrder.None
|| this.column == null) return 0;
ListViewItem ix = x as ListViewItem;
ListViewItem iy = y as ListViewItem;
if (ix.ListView != this.list || iy.ListView != this.list)
throw new Exception("The event sender does not match the list");
return
(this.list.Sorting==SortOrder.Ascending ? 1:-1) *
this[this.column](ix.SubItems[this.column.Index].Text,
iy.SubItems[this.column.Index].Text);
}
ListViewSorter
already has standard comparers (as static
methods) for strings, numbers, dates.
History
- 23 May 2003 - updated download
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.