0
votes

I have a listview that consists of three columns, one of those is a 'status' column. I want the backcolor of the cell in this column to color either green of red based on the value that's in there, but so far I haven't been able to find the right solution.

I've found a lot information on applying a backcolor to a full row, but nothing yet on doing so for a cell with a specific value. Nothing that seems to work, at least.

if (emp.SubItems[2].ToString() == "AANWEZIG")
                {
                    emp.BackColor = Color.Green;
                }
1
try changing it when its drawnBugFinder
That's what it already does; it's inside a method, that is invoked upon launch. Or is that not what you're referring to?Tom
No, theres a drawitem method, its normal to change colours etc there.BugFinder

1 Answers

0
votes

First, when you're creating your ListViewItem instances, you need to set ListViewItem.UseItemStyleForSubItems to false. This way, the ListViewItem's Font, ForeColor, and BackColor won't be used for all the subitems.

Second, your if statement needs to check the ListViewSubItem.Text property, rather than the result of ToString. ToString doesn't just return the text.

Finally, set ListViewSubItem.BackColor based on the text.

private void LoadListView()
{
   // Build up the ListViewItem that you're calling emp in your original question...
   emp.UseItemStyleForSubItems = false;
   if (emp.SubItems[2].Text == "AANWEZIG")
   {
      emp.SubItems[2].BackColor = Color.Green;
   }
}