For Each itm As ListViewItem In ListView1.Items
        If itm.Text <= CStr(56) Then
            itm.BackColor = Color.Orange
        Else
            itm.BackColor = ListView1.BackColor
        End If
    Next

this codes change background color of all items in listview....but i just want to change the forecolor mu value of stocks in listview in a column stocks..not all column..pls guys help me:(

Dani AI

Generated

The original snippet from compares the item's Text as a string and sets the item-level BackColor, so the whole row changes. String comparison is the culprit (lexical ordering), and item-level colors affect every column. was correct to point at per‑subitem styling, and reminded that ForeColor is often what is needed. Below is a concise VB.NET pattern that sets only the stock column's ForeColor after parsing the value.

' 0-based index of the column that contains the stock quantity
Dim stockCol As Integer = 2
Dim lowThreshold As Integer = 56

ListView1.BeginUpdate()
For Each li As ListViewItem In ListView1.Items
    li.UseItemStyleForSubItems = False   ' allow per-subitem colors

    Dim textVal As String = li.SubItems(stockCol).Text
    Dim qty As Integer
    If Integer.TryParse(textVal, qty) Then
        If qty <= lowThreshold Then
            li.SubItems(stockCol).ForeColor = Color.Orange
        Else
            li.SubItems(stockCol).ForeColor = ListView1.ForeColor
        End If
    Else
        li.SubItems(stockCol).ForeColor = ListView1.ForeColor
    End If
Next
ListView1.EndUpdate()

Notes and troubleshooting

  • ListView must be in Details view for SubItems to be visible. SubItems(0) is the same text as the item.
  • Selection drawing will usually override custom colors; to keep per-cell colors visible when a row is selected, use OwnerDraw and handle DrawSubItem (or switch to a DataGridView as suggested for full cell control).
  • Use BeginUpdate/EndUpdate to avoid flicker for large lists.
  • Ensure the correct column index and use TryParse (or Decimal.TryParse with NumberStyles/CultureInfo if values include decimals/commas).

This approach changes only the target column and avoids the string-comparison and whole-row coloring problems in the original post.

Recommended Answers

All 5 Replies

forecolor mu value of stocks in listview in a column stocks

In C# this works for SubItems. Am sure you can convert it to what you need in VB.NET:

lvItem.UseItemStyleForSubItems = false;
lvItem.SubItems[2].BackColor = Color.Orange; // 2 should be the right column index

thank you very much 4 ur codes:)..but not working..
my problem is this codes select all items ni all column..
( For Each itm As ListViewItem In ListView1.Items) 'select all items

i want to select only one column to change a backcolor:)

tnx guys godblessyou:) hope u help me to solve this:)

Use a DataGridView rather than a ListView. That would allow you to change colours of individual cells in any way that you want. To change an individual cell you code

dgvData.Rows(row).Cells(col).Style.BackColor = Color.Thistle

replacing row& col with the 0-relative row and column numbers.

Simply replace BackColor with ForeColor, this will color your matching Item with a specified color from the code you posted.

Sorry. That's the problem with cut and paste (and old neurons). I forgot to change Back to Fore.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.