Click here to Skip to main content
15,889,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There is 6 rows and 3 columns in my datagridview. I tried to check empty fields with this code:

VB
For r As Integer = 0 To 5 Step 1
           For c As Integer = 1 To 3 Step 1
               If mydgv(c, r).Value.ToString = "" Or mydgv(c, r).Value.ToString = "0" Then

                   MsgBox("Field can not be zero or empty")
               Else
                   'do something
               End If
           Next c
       Next r


but I get an error:
Object reference not set to an instance of an object.
What is wrong?
Thanks in advance.
Zlatkodo
Posted
Updated 9-Sep-13 18:51pm
v2
Comments
Sergey Alexandrovich Kryukov 10-Sep-13 0:40am    
I have no gut to answer to this. Who told you that mydgv(c, r) or mydgv(c, r).Value is not null? Why not checking for null — it would certainly indicate that the cell is null (cell, not "field")? Why doing ToString?!!!
I hope that would be enough to solve it...
—SA

or you can use this code

VB
For r As Integer = 0 To 5 Step 1
           For c As Integer = 1 To 3 Step 1
               If Convert.ToString(mydgv(c, r).Value) = "" Or Convert.ToString(mydgv(c, r).Value) = "0" Then

                   MsgBox("Field can not be zero or empty")
               Else
                   'do something
               End If
           Next c
       Next r
 
Share this answer
 
- I get a multiple messages ( as many as is number of empty cells + zero cells).
- Even with Exit For I get a multiple messages (as many as is the row number)
- In some cases code "do something" even with some cells value = 0 or empty.
Is there any other way to validate cell content?
Thanks in advance.


For r As Integer = 0 To 5 Step 1
          For c As Integer = 1 To 3 Step 1
              If Convert.ToString(a(c, r).Value) = "" Or Convert.ToString(a(c, r).Value) = "0" Then
 
                  MsgBox("Field can not be zero or empty")
                  ' Exit For
              Else
                  'do something
                
              End If
          Next c
      Next r
 
Share this answer
 
v2

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