user8189
user8189

Reputation: 101

How to dynamically change ListView color (skip one line)

Using VB.Net and tried many times to achieve this but couldn't get it worked.

I just need to achieve the below on a ListView

dim myRow as string

For Each myRow in ListView

  ListView1.BackColor = Color.Blue

Next

ListView image

ListView1

Upvotes: 0

Views: 87

Answers (2)

M_Idrees
M_Idrees

Reputation: 2172

You can use BackColor property for ListViewItem.

Dim i As Integer
For Each lvi As ListViewItem In ListView.Items
    If i Mod 2 = 0
        lvi.BackColor = Color.Gold
    End If
    i += 1   
Next

I have following example working:

ListView1.Items.Add("test1")
        ListView1.Items.Add("test2")
        ListView1.Items.Add("test3")
        ListView1.Items.Add("test4")

        Dim i As Integer

        For Each lvi As ListViewItem In ListView1.Items
            lvi.SubItems.Add("s1")
            lvi.SubItems.Add("s2")
        Next


        For Each lvi As ListViewItem In ListView1.Items
            If i Mod 2 = 0 Then
                lvi.BackColor = Color.Gold
            End If
            i += 1
        Next

I have also added two 3 columns with Columns property in designer mode, and set View property equal to Details.

Upvotes: 0

gmiley
gmiley

Reputation: 6604

You can use the Mod operator.

    Dim myListView As ListView
    Dim myRow As ListViewItem
    Dim rowCnt As Integer = 0
    For Each myRow In myListView.Items
        If rowCnt Mod 2 = 0 Then
            myRow.BackColor = Color.Blue
        Else
            myRow.BackColor = Color.Gray
        End If
        rowCnt = rowCnt + 1
    Next

Upvotes: 2

Related Questions