[.Net] Trier une ListView
Class ListViewItemComparer
Implements IComparer
Private m_ColumnNumber As Integer
Private m_SortOrder As SortOrder
Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder)
m_ColumnNumber = column_number
m_SortOrder = sort_order
End Sub
' Compare the items in the appropriate column
' for objects x and y.
Public Function CompareXY(ByVal x As Object, ByVal y As Object) _
As Integer Implements System.Collections.IComparer.Compare
Dim item_x As ListViewItem = DirectCast(x, ListViewItem)
Dim item_y As ListViewItem = DirectCast(y, ListViewItem)
Try
'si la colonne est de type string
If IsNumeric(item_x.SubItems(m_ColumnNumber).Text) Then
'si la colonne est de type double
Dim valeur1 As Double
If item_x.SubItems.Count <= m_ColumnNumber Then
valeur1 = 0
Else
valeur1 = CDbl(item_x.SubItems(m_ColumnNumber).Text)
End If
Dim valeur2 As Double
If item_y.SubItems.Count <= m_ColumnNumber Then
valeur2 = 0
Else
valeur2 = CDbl(item_y.SubItems(m_ColumnNumber).Text)
End If
' Compare them.
If m_SortOrder = SortOrder.Ascending Then
If valeur1 > valeur2 Then
Return 1
ElseIf valeur1 < valeur2 Then
Return -1
Else
Return 0
End If
Else
If valeur2 > valeur1 Then
Return 1
ElseIf valeur2 < valeur1 Then
Return -1
Else
Return 0
End If
End If
ElseIf IsDate(item_x.SubItems(m_ColumnNumber).Text) Then
'si la colonne est de type date
Dim valeur1 As Date
If item_x.SubItems.Count <= m_ColumnNumber Then
valeur1 = ""
Else
valeur1 = item_x.SubItems(m_ColumnNumber).Text
End If
Dim valeur2 As Date
If item_y.SubItems.Count <= m_ColumnNumber Then
valeur2 = ""
Else
valeur2 = item_y.SubItems(m_ColumnNumber).Text
End If
' Compare them.
If m_SortOrder = SortOrder.Ascending Then
Return Date.Compare(valeur1, valeur2)
Else
Return Date.Compare(valeur2, valeur1)
End If
Else
'si la colonne est de type chaine de caractere
Dim valeur1 As String
If item_x.SubItems.Count <= m_ColumnNumber Then
valeur1 = ""
Else
valeur1 = item_x.SubItems(m_ColumnNumber).Text
End If
Dim valeur2 As String
If item_y.SubItems.Count <= m_ColumnNumber Then
valeur2 = ""
Else
valeur2 = item_y.SubItems(m_ColumnNumber).Text
End If
' Compare them.
If m_SortOrder = SortOrder.Ascending Then
Return String.Compare(valeur1, valeur2)
Else
Return String.Compare(valeur2, valeur1)
End If
End If
Catch ex As Exception
Finally
End Try
End Function
End Class
Private Sub lstvClients_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lstvClients.ColumnClick
'trier la listview suite à des lenteurs sur la listview le Trie a été mis en commentaire
TrierListview(Me.lstvClients, e.Column)
End Sub
Public Sub TrierListview(ByRef p_listview As ListView, ByVal p_colTrier As Integer, Optional ByVal p_forcerAsc As Boolean = False)
For indice As Integer = 0 To p_listview.Columns.Count - 1
p_listview.Columns(indice).ImageIndex = -1
Next
If p_forcerAsc = True Then
p_listview.Sorting = SortOrder.Ascending
p_listview.Columns(p_colTrier).ImageIndex = 0
Else
If p_listview.Sorting = SortOrder.Ascending Then
p_listview.Sorting = SortOrder.Descending
p_listview.Columns(p_colTrier).ImageIndex = 1
Else
p_listview.Sorting = SortOrder.Ascending
p_listview.Columns(p_colTrier).ImageIndex = 0
End If
End If
p_listview.ListViewItemSorter = New ListViewItemComparer(p_colTrier, p_listview.Sorting)
End Sub