Error when displaying or testing for blank cell

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
Hi again.
I have a partially populated datagridview called grd1. I want to test if a cell is empty before deleting the unbound record. I highlight the row containing the record and click on the delete button:

Private Sub cmdUserDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUserDelete.Click

If grd1.Item(0, grd1.CurrentCellAddress.Y).Value.ToString = "" Then
MsgBox("Please choose a valid user to delete.", MsgBoxStyle.Information, mCompanyName)
Exit Sub
End If

'OK, now delete the record.
//Delete etc.

End Sub

If column 0 has a value, no problem. If column 0 is blank, the user is trying to delete a blank row (it happens!), but the system returns the error:
NullReferenceException: Object reference not set to an instance of an object.

However, it's only a blank value. I don't want to live with the error. Why is a blank cell causing this problem?
Thanks again.
 
I found that
grd1.Item(0, grd1.CurrentCellAddress.Y).Value.ToString = ""
failed because you can't have a blank .ToString.

grd1.Item(0, grd1.CurrentCellAddress.Y).Value = "" works just fine.
 
Back
Top