I got from this newsgroup an object that will allow you to make a list view
in detail mode sortable by any field. When I use it with more than a couple
of items, it gets really slow. I know it's the sorter because if I comment
out the line that turns the sortable on, it takes forever to load. Once it's
loaded, it works fine. I assume it's because it's checking the sort order
for each of the items.
I've enclosed the source code for both the thing loading the list view and
the code to do the actual sorting. I've tried attaching the object at the
end after all the data has been added but that didn't seem to help.
Any help would be appreciated.
TIA - Jeffrey
Source to load the data (lots of stuff has been removed so don't worry if it
looks a little odd - the real stuff does what it's supposed to):
lvBannerFiles.Clear()
'Me.lvBannerFiles.ListViewItemSorter = m_lvwColumnSorter
Dim lsBannerBaseDir As String
lvBannerFiles.Columns.Add("File Name", 100, HorizontalAlignment.Left)
lvBannerFiles.Columns.Add("Date Modified", 100, HorizontalAlignment.Left)
lvBannerFiles.Columns.Add("Kits Used In", 100, HorizontalAlignment.Left)
Dim lsFileName As String
Dim lsFullFileName As String
Dim lstemp As String
lsBannerBaseDir = "Junk"
lsFileName = Dir(lsBannerBaseDir)
Do While lsFileName <> ""
lsFullFileName = lsBannerBaseDir + lsFileName
Dim lFileInfo As FileInfo = New FileInfo(lsFullFileName)
Dim lvItem As ListViewItem = New ListViewItem
lvItem.Text = lsFileName
lvItem.SubItems.Add(lFileInfo.LastWriteTime.ToString)
lvBannerFiles.Items.Add(lvItem)
lsFileName = Dir()
Loop
'Me.lvBannerFiles.ListViewItemSorter = m_lvwColumnSorter
-------------------------------------------------
m_lvwColumnSorter is of type ListViewColumnSorter. Here's the code for that:
Imports System.Collections
Imports System.Windows.Forms
Public Class ListViewColumnSorter
Implements System.Collections.IComparer
Private ColumnToSort As Integer
Private OrderOfSort As SortOrder
Private ObjectCompare As CaseInsensitiveComparer
Public Sub New()
' Initialize the column to '0'.
ColumnToSort = 0
' Initialize the sort order to 'none'.
OrderOfSort = SortOrder.None
' Initialize the CaseInsensitiveComparer object.
ObjectCompare = New CaseInsensitiveComparer
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compare
Dim compareResult As Integer
Dim listviewX As ListViewItem
Dim listviewY As ListViewItem
' Cast the objects to be compared to ListViewItem objects.
listviewX = CType(x, ListViewItem)
listviewY = CType(y, ListViewItem)
' Compare the two items.
' JLB Added code to check the type that the columns are. Then
' call the comparer with the appropriate type of data. It
' will do the comparison correctly based on the data type.
If (IsNumeric(listviewX.SubItems(ColumnToSort).Text) And _
IsNumeric(listviewY.SubItems(ColumnToSort).Text)) Then
Dim liXValue As Int64 =
Convert.ToInt64(listviewX.SubItems(ColumnToSort).Text)
Dim liYValue As Int64 =
Convert.ToInt64(listviewY.SubItems(ColumnToSort).Text)
compareResult = ObjectCompare.Compare(liXValue, liYValue)
Else
If (IsDate(listviewX.SubItems(ColumnToSort).Text) And _
IsDate(listviewY.SubItems(ColumnToSort).Text)) Then
Dim ldXValue As DateTime =
Convert.ToDateTime(listviewX.SubItems(ColumnToSort).Text)
Dim ldYValue As DateTime =
Convert.ToDateTime(listviewY.SubItems(ColumnToSort).Text)
compareResult = ObjectCompare.Compare(ldXValue, ldYValue)
Else
compareResult =
ObjectCompare.Compare(listviewX.SubItems(ColumnToSort).Text,
listviewY.SubItems(ColumnToSort).Text)
End If
End If
' Calculate the correct return value based on the object
' comparison.
If (OrderOfSort = SortOrder.Ascending) Then
' Ascending sort is selected, return typical result of
' compare operation.
Return compareResult
ElseIf (OrderOfSort = SortOrder.Descending) Then
' Descending sort is selected, return negative result of
' compare operation.
Return (-compareResult)
Else
' Return '0' to indicate that they are equal.
Return 0
End If
End Function
Public Property SortColumn() As Integer
Set(ByVal Value As Integer)
ColumnToSort = Value
End Set
Get
Return ColumnToSort
End Get
End Property
Public Property Order() As SortOrder
Set(ByVal Value As SortOrder)
OrderOfSort = Value
End Set
Get
Return OrderOfSort
End Get
End Property
End Class