Search code examples
vb.netformattingrowobjectlistview

ObjectListView - FormatRow Event Not Working For ForeColor


I am trying to format a row in an ObjectListView using the FormatRow event and set the entire row a different ForeColor based on a value, however, to no avail.

My code:

Private Sub lsv_OpenTickets_FormatRow(sender As Object, e As FormatRowEventArgs) Handles lsv_OpenTickets.FormatRow
    Dim tkt As Ticket = DirectCast(e.Model, Ticket)
    If tkt.due = "Overdue" Then
        e.Item.ForeColor = Color.FromArgb(252, 146, 156)
    End If
End Sub

Only seems to set the first Item in the row to the specified color and every other SubItem is painted with the default color.

If I change my code to:

e.Item.BackColor = Color.FromArgb(252, 146, 156)

Then it paints the entire row correctly as a back color. It just doesn't seem to work for the ForeColor.

Maybe I'm doing something wrong? Or missed something maybe?


Solution

  • Ok, so it turns out that the e.Item.ForeColor only seems to apply formatting to the first SubItem in the row - not all SubItems like I would've thought. Not sure if this is intentional or a bug or if I'm coding something incorrectly, but to me 'Item' corresponds to the entire row, whereas SubItem corresponds to individual cells.

    Anyway, to overcome my issue, I modified my code to the following:

    Private Sub lsv_OpenTickets_FormatRow(sender As Object, e As FormatRowEventArgs) Handles lsv_OpenTickets.FormatRow
        Dim tkt As Ticket = DirectCast(e.Model, Ticket)
        If tkt.due = "OVERDUE" Then
            For Each sb As OLVListSubItem In e.Item.SubItems
                sb.ForeColor = Color.FromArgb(252, 146, 159)
            Next
        End If
    End Sub
    

    This now gives me the results I required and also works well with the FormatCell event, as individual cell formatting will override the row formatting.