Get Items in ListView to alternate background color in vb.net

We wanted to alternate the background color of items in the listview and make one particular item stand out from the rest. If the position in the textbox is nothing it reverts to standard blue/white background. So here is what I did……….

While dr.Read()
            i = (i + 1)
            li = lvQualificationsStandings.Items.Add(i)
            If dr.Item("QualID") <> Nothing Then
                If txtPosition.Text = Nothing Then
                    If i Mod 2 = 0 Then
                        li.BackColor = Color.White
                    Else
                        li.BackColor = Color.LightBlue
                    End If
                    li.SubItems.Add(dr("Car Number"))
                    li.SubItems.Add(dr("Driver"))
                    li.SubItems.Add(dr("QualifyingSpeed"))
                    li.SubItems.Add(dr("Status "))
                    li.SubItems.Add(dr("OnTheBubble"))
                Else
                    If i = txtPosition.Text Then
                        li.BackColor = Color.Yellow ‘<<< NEW LINE
                    ElseIf i Mod 2 = 0 Then ‘<<< AMENDED LINE
                        li.BackColor = Color.White
                    Else
                        li.BackColor = Color.LightBlue
                    End If
                    li.SubItems.Add(dr("Car Number"))
                    li.SubItems.Add(dr("Driver"))
                    li.SubItems.Add(dr("QualifyingSpeed"))
                    li.SubItems.Add(dr("Status "))
                    li.SubItems.Add(dr("OnTheBubble"))
                End If
            End If

 

Consulting Requests

Feedback

Blog Front Page

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: