Click here to Skip to main content
11,633,425 members (80,298 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 8-Aug-10 19:34pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
rashidfarooq at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Why not use:
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:
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
  Permalink  
v3
Comments
rashidfarooq at 10-Aug-10 9:07am
   
Thanks a lot brother.
I works correctly.
Thanks very much again.
Member 11795859 at 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 at 15-Jul-15 10:24am
   
compare num >= Date.NOW

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

  Print Answers RSS
0 OriginalGriff 8,658
1 Sergey Alexandrovich Kryukov 8,204
2 Mika Wendelius 6,645
3 F-ES Sitecore 2,321
4 Suvendu Shekhar Giri 2,095


Advertise | Privacy | Mobile
Web01 | 2.8.150728.1 | Last Updated 11 Aug 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100