How can i change the back color of a listView for a particular value of sub items? I have a column for status and i want to change the back color of all "ACTIVE" to yellow.
Asked
Active
Viewed 1,489 times
0
-
how can i call it in the form load? – Jeric Canatuan May 10 '15 at 09:27
1 Answers
0
You want access to the SubItems
of an entry. On a non virtual list view this would go something like this:
foreach(ListViewItem lvi in listView1)
{
if(lvi.SubItems[colToCheck].Text == "ACTIVE")
{
lvi.BackColor = Color.Yellow;
}
}
Put this somewhere where the list is already filled. Since you mentioned the form load in your comment I suppose there is a method present that fills the ListView with items. Put the above snippet afterwards.
If you have a virtual list, check the corresponding item property in RetrieveVirtualItem
:
private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
var lvi = GenerateListViewItemFromTheStore(e.ItemIndex);
if (lvi != null)
{
if (lvi.SubItems[colToCheck].Text == "ACTIVE")
lvi.BackColor = Color.Yellow;
}
else
e.Item = new ListViewItem();
}
In both snippets colToCheck
is the column index of the column to check for Text equalling ACTIVE.

kaedinger
- 94
- 6