ListView order

I have a ListView control with "Details" property.

The ListView has some columns.

If i click a column header i want the items to be ordered ascending / descending by the text of this column (like in windows folders).
[258 byte] By [nikos_22] at [2007-12-22]
# 1

Hi,

You need to create a class that implements ICompare, then creating an instance of it and assign it to the ListViewSort property.

Here is an example:

public class ListSorter : IComparer
{

public int Compare(object x, object y)
{
ListViewItem Current = (ListViewItem) x;
ListViewItem Comparer = (ListViewItem) y;

//
// Here you do your comprisons (for example)
//
return Current.SubItems[1].Text.CompareTo(Comparer .SubItems[1].Text);
}
}

Then on your list you assign the sorter when they click the column:

this.lstAuctions.Sorting = SortOrder.Ascending;
this.lstAuctions.ListViewItemSorter = new ListSorter();

Hope this helps

Regards

SalvaPatuel at 2007-8-30 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified