locked
Change background color of alternating rows in listview RRS feed

  • Question

  • Hi all,

    Does anyone know how to change the background color of alternating rows in a listview in Visual Basic 2013?

    Best regards,

    Randy Boulter

    Monday, March 2, 2015 9:44 AM

Answers

  • Dim iView As Integer = ListView1.Items.Count - 1
            For i = 1 To iView Step 2
                ListView1.Items(i).UseItemStyleForSubItems = True
                ListView1.Items(i).BackColor = Drawing.Color.WhiteSmoke
            Next i

    • Proposed as answer by Cor Ligthert Monday, March 2, 2015 10:15 AM
    • Marked as answer by Randy Boulter Monday, March 2, 2015 2:46 PM
    Monday, March 2, 2015 9:53 AM
  • You can set the OwnerDraw property of the ListView to true and use the DrawItem event:

        Private Sub ListView1_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListView1.DrawItem
            e.DrawDefault = True
            If (e.ItemIndex Mod 2) = 1 Then
                e.Item.BackColor = Color.FromArgb(220, 220, 220)
                e.Item.UseItemStyleForSubItems = True
            End If
    
        End Sub

    • Proposed as answer by Rbie Monday, March 2, 2015 10:05 AM
    • Marked as answer by Randy Boulter Monday, March 2, 2015 2:47 PM
    Monday, March 2, 2015 9:58 AM

All replies

  • Dim iView As Integer = ListView1.Items.Count - 1
            For i = 1 To iView Step 2
                ListView1.Items(i).UseItemStyleForSubItems = True
                ListView1.Items(i).BackColor = Drawing.Color.WhiteSmoke
            Next i

    • Proposed as answer by Cor Ligthert Monday, March 2, 2015 10:15 AM
    • Marked as answer by Randy Boulter Monday, March 2, 2015 2:46 PM
    Monday, March 2, 2015 9:53 AM
  • You can set the OwnerDraw property of the ListView to true and use the DrawItem event:

        Private Sub ListView1_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListView1.DrawItem
            e.DrawDefault = True
            If (e.ItemIndex Mod 2) = 1 Then
                e.Item.BackColor = Color.FromArgb(220, 220, 220)
                e.Item.UseItemStyleForSubItems = True
            End If
    
        End Sub

    • Proposed as answer by Rbie Monday, March 2, 2015 10:05 AM
    • Marked as answer by Randy Boulter Monday, March 2, 2015 2:47 PM
    Monday, March 2, 2015 9:58 AM
  • Thanks, both solutions worked for me.  

    Best regards,

    Randy Boulter

    Monday, March 2, 2015 10:12 AM
  • Thanks, both solutions worked for me.  

    Best regards,

    Randy Boulter

    Randy,

    You should mark the post(s) as the answer using the mark as answer link below the post.

    :)

    Monday, March 2, 2015 12:30 PM