vb.net 更改列表视图子项的前景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20593025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:09:01  来源:igfitidea点击:

Change listview subitem fore color

vb.netvisual-studio-2010visual-studio

提问by user1532468

I am trying to change the fore color for certain columns based on there value in a listview subitem. I have tried various options and looked at various posts on SO but nothing seems to work.

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

In my present code, instead of changing dr(9) it is changing dr(0). Where have I gone wrong. Thanks

在我目前的代码中,不是更改 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

回答by Bj?rn-Roger Kringsj?

You must set the UseItemStyleForSubItemsproperty of each ListViewItemto False.

您必须将UseItemStyleForSubItemseach的属性设置ListViewItemFalse

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

EDIT

编辑

I see that you are referring to a subitem that doesn't exists:

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

.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!

Try change the code to:

尝试将代码更改为:

Dim list As New List(Of ListViewItem)
Dim item As ListViewItem = Nothing
Dim subItems As ListViewItem.ListViewSubItem() = Nothing

Using dr = oledbCmd.ExecuteReader()

    While dr.Read()

        item = New ListViewItem()
        item.UseItemStyleForSubItems = False
        item.Text = dr(0).ToString()

        subItems = New ListViewItem.ListViewSubItem(3 - 1) {New ListViewItem.ListViewSubItem(), New ListViewItem.ListViewSubItem(), New ListViewItem.ListViewSubItem()}
        subItems(0).Text = CDate(dr(5)).ToShortDateString()
        subItems(1).Text = dr(1).ToString()

        Select Case dr(3).ToString
            Case "D" : subItems(2).Text = "Destroyed"
            Case "O" : subItems(2).Text = "Out"
            Case "I" : subItems(2).Text = "Intake"
            Case Else : subItems(2).Text = ""
        End Select

        If IsDBNull(dr(9)) Then
            subItems(3).Text = "O/S"
            subItems(3).ForeColor = Color.DarkRed
        ElseIf dr(9) IsNot "DEMO" Then
            subItems(3).Text = "Done"
        Else
            subItems(3).Text = dr(9).ToString()
        End If

        item.SubItems.AddRange(subItems)
        list.Add(item)

    End While

End Using

Me.lvRequests.Items.AddRange(list.ToArray())