Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change listview subitem fore color

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.

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

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
like image 679
user1532468 Avatar asked Dec 15 '13 09:12

user1532468


1 Answers

You must set the UseItemStyleForSubItems property of each ListViewItem to False.

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())
like image 199
Bjørn-Roger Kringsjå Avatar answered Oct 14 '22 08:10

Bjørn-Roger Kringsjå