【问题标题】:Change listview subitem fore color更改列表视图子项前景色
【发布时间】:2014-01-02 18:53:55
【问题描述】:

我正在尝试根据列表视图子项中的值更改某些列的前景色。我尝试了各种选项并查看了关于 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

【问题讨论】:

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


【解决方案1】:

您必须将每个ListViewItemUseItemStyleForSubItems 属性设置为False

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!

尝试将代码更改为:

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())

【讨论】:

  • @user1532468 抱歉,我现在看到了。只是这通常是问题所在;)
  • np Bjorn,欢迎来到我的世界 :-) 我很困惑为什么它会改变 dr(0) 而不是 dr(9) 的颜色
  • 您确定满足If IsDBNull(dr(9)) Then 中的条件吗?
  • 是的。我认为这与 .Text = dr(0).ToString() 这一行有关,当我在其他子项中设置 .text 时,它指的是那个。
  • Bjorn,“对象引用未设置为对象实例”的错误。在这一行虽然 dr.Read(),谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多