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;
}
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;
}
}