Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to change the color of a cell(back or fore color) of listview item according to item value with if condition.

I do it but it is not work and show no error
Please help me......

What I have tried:

VB
Private Sub btnaddcat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddcat.Click
        For k = 0 To ListView1.Items.Count - 1
            If ListView1.Items(k).SubItems(6).Text > 10000 Then
                ListView1.Items(k).SubItems(6).ForeColor = System.Drawing.Color.Red
            End If
        Next k
    End Sub
Posted
Updated 18-Sep-18 11:13am
Comments
ZurdoDev 18-Sep-18 13:27pm    
Debug your code and see what is happening.
Jayanta Modak 18-Sep-18 13:42pm    
Sir I debug but show no error massage it debug 1st to last data
ZurdoDev 18-Sep-18 13:45pm    
Debug means to step through the code line by line and check the values of your controls and variables.

For example, does the code ever execute the line of code that changes the color? You need to debug and make sure. It's quite possible the problem is with your if statement, especially since you are trying to compare text to numbers.
phil.o 18-Sep-18 17:09pm    
If ListView1.Items(k).SubItems(6).Text > 10000 Then

I am even surprised this compiles, as there is no "greater than" comparison operator between a string and an integer value. Time to study the different types and their proper usage :)

1 solution

You cannot compare a string and an integer value the way you tried to.
One possible solution could be to parse the string value to an integer before the comparison:
VB.NET
Dim value As Integer
If Integer.TryParse(ListView1.Items(k).SubItems(6).Text, value) Then
   If value > 10000 Then
      '' ...
   End If
End If
 
Share this answer
 
Comments
Jayanta Modak 18-Sep-18 23:10pm    
Now my full code is
Private Sub btnaddcat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddcat.Click
For k = 0 To ListView1.Items.Count - 1
'If ListView1.Items(k).SubItems(6).Text > 10000 Then
' ListView1.Items(k).SubItems(6).ForeColor = System.Drawing.Color.Red
'End If
Dim value As Integer
If Integer.TryParse(ListView1.Items(k).SubItems(6).Text, value) Then
If value > 10000 Then
ListView1.Items(k).SubItems(6).ForeColor = System.Drawing.Color.Red
End If
End If
Next k
End Sub
It is does not work and not debug the full code, After this line
" If Integer.TryParse(ListView1.Items(k).SubItems(6).Text, value) Then" go to the

End If
Next k
do not debug or read the middle lines.
Please help me sir
Sir Check with my code it work fine but not change the color of cell
my code is ---
For k = 0 To ListView1.Items.Count - 1
If ListView1.Items(k).SubItems(6).Text > 100 Then
ListView1.Items(k).SubItems(6).ForeColor = System.Drawing.Color.Red
MsgBox("hi test code yes")
Else
MsgBox("hi test code no")
End If
Next k

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900