2

I tried to add text color to any sub item in the listview by the index that the method get from the array

for (int i = 0; i < sizes.Length; ++i)
            {
                if (sizes[i] == 1)
                {
                    Item.SubItems.Add("In Stock");
                }
                else if (sizes[i] == 0)
                {
                    Item.SubItems.Add("Out Of Stock");
                }
                else if (sizes[i] == 2)
                {
                    Item.SubItems.Add("Less Than 3");
                }
                else if (sizes[i] == 5)
                {
                    Item.SubItems.Add("Less Than 5");
                }
                else if (sizes[i] == 10)
                {
                    Item.SubItems.Add("Less Than 10");
                }
            }

            ProductListView.Items.Add(Item);
        }

if the size in stock the subitem color change to green and if the size is out of stock the subitem color will change to red

thanks

Nadav
  • 65
  • 9

1 Answers1

3

The Add method returns the subItem,
So you can change the subItem color like this:

var subItem = Item.SubItems.Add("In Stock");
subItem.ForeColor = Color.Green;
// subItem.BackColor = Color.Red;
Ofir Winegarten
  • 9,215
  • 2
  • 21
  • 27
  • 1
    Don't forget to turn off `UseItemStyleForSubItems` for each item! – TaW Mar 26 '17 at 15:15
  • Thanks for the `itemUser.UseItemStyleForSubItems = false;` option! The color can still be assigned like this `itemUser.SubItems.Add("Text", System.Drawing.Color.Empty, System.Drawing.Color.Blue, null);` – KUL Dec 09 '21 at 23:49