1. Create a Class that implements
IComparer.Compare
See the example in the
ListView.ListViewItemSorter
property documentation.
2. Set the
ListView.ListViewItemSorter
[
^] property to the
IComparer.Compare
Class.
Quote:
You can use this property to provide custom sorting, for example, when a column header is clicked in the details view. To do this, create a class that implements the IComparer interface and provides a constructor that accepts the index of a column to sort by. You can then implement a handler for the ColumnClick event that creates an instance of this class using the index of the clicked column. When you set the ListViewItemSorter
property to the new instance, the ListView control is automatically sorted using the specified object. Subsequent calls to the Sort method use the same object.
A tip: Put all of the statements within the
IComparer.Compare
Class's
Compare
function within a
Try..Catch
statement and, after you have debugged the code,
Return 0
in the
Catch
block. In my experience, I had to do this because sometimes the
Compare
function was called and an error occurred during the
Compare
function. It has been awhile since I experienced this so I don't remember the exact circumstances but I remember it was when the
ListView
control was in a state where it shouldn't have been trying to sort the rows. I think it might have been because the
Compare
function was called when the
ListViewItemSorter
was set but I am not sure.