Test to see if datagridview cell is null

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Before saving information in a datagridview to a database I'm trying to
verify tha certain cells are not blank (dbNull). When I run this code:

If IsDBNull(Me.dgvHistory(2, Me.dgvHistory.CurrentCell.RowIndex).Value)
Then
Me.dgvHistory(2, Me.dgvHistory.CurrentCell.RowIndex).Value =
Nothing
End If

This error apears when the first line is executed:
System.NullReferenceException
{"Object reference not set to an instance of an object."}

I've spent a few hours work on this, but nothis seems to work. Any
suggestions would be greatly appreciated.
 
Something in that statement has a value of Nothing. The first thing
to do is to use the debugger to see what has the null reference. You
can't fix it without knowing what is wrong.

When you get the error, what is the value of:
Me.dgvHistory.CurrentCell
Me.dgvHistory.CurrentCell.RowIndex
Me.dgvHistory(2, Me.dgvHistory.CurrentCell.RowIndex)

Most likely CurrentCell is Nothing.
 
I use
If DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value.ToString.Length
= 0 Then ...

It seems to work.
 
Back
Top