Click here to Skip to main content
15,849,829 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear Friends.

I am working on a project in VB.Net and I have made a datagridview to show output.
I have three columns in DataGridView. which are as under;
1. SerialNo
2. Amount
3. Date

Now, I want to Highlight Those rows of DataGridView in which the Amount Field has a value more than 10,000.
So, Is there any Mechanism in VB.Net?

Waiting for answer.
Posted
Updated 16-Aug-20 15:58pm

For Each row As DataGridViewRow In DataGridView1.Rows
            Dim val As Integer =                      Integer.Parse(row.Cells(0).Value.ToString()) 
            If val > 10000 Then
                row.DefaultCellStyle.BackColor = Color.Red
            End If
        Next

//row.Cells(1) is your Amount Column
 
Share this answer
 
v2
Comments
rashidfarooq 10-Aug-10 8:19am    
Brother Thanks for giving me such useful answer.
But there is a problem.
I have applied this code and it works fine but when it checks the last row of DatagridView (Which is Empty) it gives me the following error.

object reference not set to an instance of an object

Then I debug this code line by line and when the Last iteration (Which is now on the blank row of Datagridview) reaches to this line

Dim val As Integer = Integer.Parse(row.Cells(0).Value.ToString())

The above mentioned error appears.

So, Please guide me how can I solve this Problem.

I will be thankful to you.
MatthysDT 11-Aug-10 2:38am    
Simple, just don't run your code on the last line. So if the row index = DatagridView1.NewRowIndex don't format it.
Why not use:
VB
For f As Integer = 0 To DataGridView1.Rows.Count - 1
Dim num As Integer = Val(DataGridView1.Rows(f).Cells(1).Value)
If num > 10000 Then
DataGridView1.Rows(f).DefaultCellStyle.BackColor = Color.Red
End If
Next

Or:
VB
For Each row As DataGridViewRow In DataGridView1.Rows
Dim num As Integer = Val(row.Cells(1).Value)
If num > 10000 Then
row.DefaultCellStyle.BackColor = Color.Red
End If
Next
 
Share this answer
 
v3
Comments
rashidfarooq 10-Aug-10 9:07am    
Thanks a lot brother.
I works correctly.
Thanks very much again.
Member 11795859 27-Jun-15 14:52pm    
brother,how to highlight a datagridview row when today date occurs? i want to gighlight my data in datagrid view when the today's date occur
cristi.corneanu 15-Jul-15 10:24am    
compare num >= Date.NOW
Genius Homwe 13-Jun-21 19:36pm    
Thanks it worked well for me
Integer.Parse(row.Cells(0).Value.ToString())
If val > 10000 Then
row.DefaultCellStyle.BackColor = Color.Red
End If
Next
 
Share this answer
 
Comments
CHill60 17-Aug-20 4:02am    
If you are going to copy someone else's answer (by the way, that is a bad idea), at least have the sense to copy all the code!

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