Private Sub AddColumn(LV As ListView, oCol As Collection, Header As String, Width As Long, Optional Color1 As OLE_COLOR = vbBlack, Optional Color2 As OLE_COLOR = vbRed)
Dim Litem As ListItem, i As Integer, j As Integer, lCol As OLE_COLOR
i = LV.ColumnHeaders.Count
If i = 0 Then
' caption/name
LV.ColumnHeaders.Add 1, , Header
LV.ColumnHeaders(1).Width = Width
For i = 1 To oCol.Count
Set Litem = LV.ListItems.Add(, , oCol.Item(i))
lCol = IIf(i And 1, Color1, Color2)
LV.ListItems(i).ForeColor = lCol
Next i
Else
' value, on ajoute une colonne et on la remplie
LV.ColumnHeaders.Add , , Header
LV.ColumnHeaders(i + 1).Width = Width
For j = 1 To oCol.Count
Set Litem = LV.ListItems.Item(j)
Litem.SubItems(i) = Trim$(oCol.Item(j))
lCol = IIf(j And 1, Color1, Color2)
Litem.ListSubItems(i).ForeColor = lCol
Next j
End If
End Sub
' EXEMPLE D'UTILISATION
Private Sub Form_Load()
Dim c1 As New Collection
Dim c2 As New Collection
With ListView1
.LabelEdit = lvwManual
.FullRowSelect = True
.View = lvwReport
.GridLines = True
End With
c1.Add "voici": c1.Add "une": c1.Add "première": c1.Add "colonne"
Call AddColumn(ListView1, c1, "Colonne 1", 800)
c2.Add "et voici": c2.Add "une": c2.Add "deuxième": c2.Add "colonne"
Call AddColumn(ListView1, c2, "Colonne 2", 900, &H8000&, &HFF0000)
Set c2 = Nothing
Set c1 = Nothing
End Sub