更改listview子项前颜色

时间:2013-12-15 09:20:49

标签: vb.net visual-studio-2010 visual-studio

我正在尝试根据listview子项中的值更改某些列的前景色。我已经尝试了各种选项并查看了SO上的各种帖子,但似乎没有任何效果。

在我现在的代码中,不是改变dr(9)而是改变dr(0)。我哪里出错了。感谢

Using dr = oledbCmd.ExecuteReader()


            'clear items in the list before populating with new values
            'lvRequests.Items.Clear()

            While dr.Read()
                If dr.HasRows Then
                    Dim LVI As New ListViewItem

                    With LVI

                        .UseItemStyleForSubItems = False
                        .Text = dr(0).ToString()
                        .SubItems.Add(CDate(dr(5)).ToShortDateString())
                        .SubItems.Add(dr(1).ToString())
                        .SubItems.Add(dr(3).ToString())

                        If dr(3).ToString = "D" Then
                            .SubItems(3).Text = "Destroyed"

                        ElseIf dr(3).ToString = "O" Then
                            .SubItems(3).Text = "Out"

                        ElseIf dr(3).ToString = "I" Then
                            .SubItems(3).Text = "Intake"
                        End If

                        .SubItems.Add(dr(9).ToString())

                        If IsDBNull(dr(9)) Then
                            .SubItems(LVI.SubItems.Count - 1).Text = "O/S"
                            .ForeColor = Color.DarkRed


                        ElseIf dr(9) IsNot "DEMO" Then
                            .SubItems(LVI.SubItems.Count - 1).Text = "Done"

                        End If

                    End With
                    lvRequests.Items.Add(LVI)

                    lvcount += 1

                End If

            End While
        End Using

1 个答案:

答案 0 :(得分:7)

您必须将每个UseItemStyleForSubItems的{​​{1}}属性设置为 False

ListViewItem

修改

我看到你指的是一个不存在的子项目:

For i As Integer = 0 To (Me.ListView1.Items.Count - 1)
    Me.ListView1.Items(i).UseItemStyleForSubItems = False
Next

尝试将代码更改为:

.Text = dr(0).ToString()
.SubItems.Add(CDate(dr(5)).ToShortDateString()) '< Index: 0
.SubItems.Add(dr(1).ToString()) '< Index: 1
.SubItems.Add(dr(3).ToString()) '< Index: 2

If dr(3).ToString = "D" Then
.SubItems(3).Text = "Destroyed" '<- No subitems with index 3 exists!