Hi,
I would like to add the functionality of sorting by cliking on the Headings in my ListView. Is this possible?
Does the control take care of this for you? Do you have to code it? How?
Printable View
Hi,
I would like to add the functionality of sorting by cliking on the Headings in my ListView. Is this possible?
Does the control take care of this for you? Do you have to code it? How?
You can set the Sorting property and then use the Sort method to sort the first column, ascending or descending, if you want more than that you instead have to create you own comparer and set the ListViewItemSorter. If you want an example I created a Comparer to sort dates in any column...
And to use it....Code:Class CompareByDate
Implements IComparer
Dim index As Integer
Sub New(ByVal subitemIndex As Integer)
Me.index = subitemIndex
End Sub
Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements System.Collections.IComparer.Compare
Dim item1 As ListViewItem = CType(x, ListViewItem)
Dim item2 As ListViewItem = CType(y, ListViewItem)
Return Date.Compare(Date.Parse(item1.SubItems(index).Text), Date.Parse(item2.SubItems(index).Text))
End Function
End Class
Code:ListView1.ListViewItemSorter = New CompareByDate(0)
Thankyou,
I have found a MSDN article entitled:
Sorting ListView Items by Column Using Windows Forms
That explains it pretty well, including code on how to sort by date.
Thanks,