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 of another ForeColor based on the 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
It seems that the first item in the row is set to the specified color, and every other SubItem is colored with the default color.
If I change my code to:
e.Item.BackColor = Color.FromArgb(252, 146, 156)
Then it correctly draws the entire line as the back color. It just doesn't work for ForeColor.
Am I doing something wrong? Or missed something?
source to share
So it turns out that e.Item.ForeColor seems to apply formatting to the first SubItem in the line - not all SubItems as I thought. Not sure if this is intentional or a bug, or if I am coding something wrong, but for me "Item" matches the whole string, whereas SubItem matches individual cells.
Anyway, to overcome my problem, I changed 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 want and also works well with the FormatCell event as formatting individual cells overrides string formatting.
source to share