1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
Public Class ListViewItemComparer
Implements IComparer
Private col As Integer
Private sortOrder As SortOrder
Private etatn As Integer
Public Sub New(ByVal column As Integer, ByVal s As SortOrder, ByVal etanum As Integer)
sortOrder = s
etatn = etanum
'repérer de qu'elle listview avec la variable etanum et puis attribuer à la colonne le type de trier qu'il lui faut
Select Case etatn
Case 1 'produit
If column = 5 Or column = 3 Then
col = 1
Else
col = 2
End If
Case 2
col = 2
Case 3
If column = 1 Then
col = 2
Else
If column = 2 Then
col = 2
Else
If column = 3 Then
col = 2
Else
If column = 4 Then
col = 2
Else
If column = 5 Then
col = 1
Else
If column = 6 Then
col = 1
Else
If column = 7 Then
col = 1
End If
End If
End If
End If
End If
End If
End If
Case 4
If column = 0 Or column = 3 Then
col = 1
Else
col = 2
End If
Case 5
If column = 0 Or column = 3 Then
col = 1
Else
col = 2
End If
End Select
End Sub
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Select Case col
Case 0 'Index de la colonne contenant des dates
'sortOrder = Windows.Forms.SortOrder.Ascending
If sortOrder = Windows.Forms.SortOrder.Descending Then
Return Date.Compare(CType(y, ListViewItem).SubItems(col).Text, CType(x, ListViewItem).SubItems(col).Text) 'Inverser y et x pour un tri chronologique
sortOrder = Windows.Forms.SortOrder.Descending
Else
Return Date.Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text) 'Inverser y et x pour un tri chronologique
sortOrder = Windows.Forms.SortOrder.Ascending
End If
Case 1 'Index de la colonne contenant des valeurs numeriques
Dim s1 As String = CType(x, ListViewItem).SubItems(col).Text
Dim s2 As String = CType(y, ListViewItem).SubItems(col).Text
Dim l As Long
Dim d As Double
If Long.TryParse(s1, l) AndAlso Long.TryParse(s2, l) Then
If Long.Parse(s1) < Long.Parse(s2) Then
If sortOrder = Windows.Forms.SortOrder.Ascending Then
Return -1
Else
Return 1
End If
Else
If sortOrder = Windows.Forms.SortOrder.Ascending Then
Return 1
Else
Return -1
End If
End If
ElseIf Double.TryParse(s1, d) AndAlso Double.TryParse(s2, d) Then
'Idem avec Double
Else
If sortOrder = Windows.Forms.SortOrder.Ascending Then
Return String.Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
Else
Return String.Compare(CType(y, ListViewItem).SubItems(col).Text, CType(x, ListViewItem).SubItems(col).Text)
End If
End If
Case 2 'Index de colonnne contient des valeurs de type string
If sortOrder = Windows.Forms.SortOrder.Descending Then
Return String.Compare(CType(x, ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
Else
Return String.Compare(CType(y, ListViewItem).SubItems(col).Text, CType(x, ListViewItem).SubItems(col).Text)
End If
End Select
End Function |
Partager