如何动态更改ListView颜色(跳过一行)

时间:2017-08-14 15:32:40

标签: .net vb.net

使用VB.Net并多次尝试实现这一目标但无法实现。

我只需要在ListView上实现以下功能



dim myRow as string

For Each myRow in ListView

  ListView1.BackColor = Color.Blue

Next




ListView image

ListView1

2 个答案:

答案 0 :(得分:2)

您可以使用Mod运算符。

    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

答案 1 :(得分:0)

您可以将BackColor属性用于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

我有以下示例工作:

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

我还在设计器模式下添加了两个带有Columns属性的3列,并将View属性设置为Details