Problem with Listview sorting

khalik

Member
Joined
Jan 25, 2006
Messages
9
Programming Experience
5-10
Hi All
i have a listview control in my application where i display the search results.
to allow sorting i have the following procedure
VB.NET:
Public Function Compare(ByVal x As Object, ByVal y As Object) _
As Integer Implements System.Collections.IComparer.Compare
Dim lv1 As ListViewItem = x
Dim lv2 As ListViewItem = y
If mNumeric Then
If m_bAltSort = True Then
Return CLng(lv1.SubItems(mColumn).Text) + CDbl(lv2.SubItems(mColumn).Text)
Else
Return CDbl(lv1.SubItems(mColumn).Text) - CDbl(lv2.SubItems(mColumn).Text)
End If
ElseIf mDate Then
Dim firstDate As System.DateTime
Dim secondDate As System.DateTime
If lv1.SubItems(mColumn).Text = "" Then
firstDate = "1/1/0001"
Else
firstDate = DateTime.Parse(CType(lv1, ListViewItem).SubItems(mColumn).Text)
End If
If lv2.SubItems(mColumn).Text = "" Then
secondDate = "1/1/0001"
Else
secondDate = DateTime.Parse(CType(lv2, ListViewItem).SubItems(mColumn).Text)
End If
 
' Compare the two dates.
If m_bAltSort = True Then
Return DateTime.Compare(secondDate, firstDate)
Else
Return DateTime.Compare(firstDate, secondDate)
End If
' If neither compared object has a valid date format, 
' compare as a string.
Else
If m_bAltSort = True Then
Return String.Compare(lv2.SubItems(mColumn).Text, lv1.SubItems(mColumn).Text)
Else
Return String.Compare(lv1.SubItems(mColumn).Text, lv2.SubItems(mColumn).Text)
End If
End If
End Function
to call this..
VB.NET:
Private Sub lvwCustomers_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lvwCustomers.ColumnClick
 
Dim bAltSort As Boolean = False
Dim MySortClass As New SortClass
' Try
MySortClass.column = e.Column
If TreeviewMain.SelectedNode.Text = "Procurement Service Charges" Then
MySortClass.column = 1
If IsNumeric(lvwCustomers.Items(0).SubItems(1).Text.ToUpper.Trim) Then
MySortClass.bNumeric = True
Else
MySortClass.bNumeric = False
End If
'Find out if the user is clicking on the same column.
If mIntSelectedCol = e.Column Then
MySortClass.AltSort = True
mIntSelectedCol = -1
Else
mIntSelectedCol = e.Column
End If
lvwCustomers.ListViewItemSorter = MySortClass
Exit Sub
Else
'Find out of we are sorting a numeric column
If IsNumeric(lvwCustomers.Items(0).SubItems(e.Column).Text.ToUpper.Trim) Then
MySortClass.bNumeric = True
ElseIf InStr(lvwCustomers.Columns(e.Column).Text, "Date") > 0 Then
MySortClass.iDate = True
Else
MySortClass.bNumeric = False
End If
'Find out if the user is clicking on the same column.
If mIntSelectedCol = e.Column Then
MySortClass.AltSort = True
mIntSelectedCol = -1
Else
mIntSelectedCol = e.Column
End If
lvwCustomers.ListViewItemSorter = MySortClass
End If
thi works fine but the problem is once i click the column header and polutate the list view compare function fires.
i need to stop the compare function from firing.
 
Last edited by a moderator:
Back
Top