Click here to Skip to main content
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.

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



Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 11 Aug 2010
Copyright © CodeProject, 1999-2014
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