how to check if the cell value is nothing or not in vb.net?
If Not DataGridView1.SelectedRows.Count = 0 Then
i = DataGridView1.SelectedRows(0).Index
If DataGridView1.Rows(i).Cells(0).Value <> Nothing Then
nam开发者_StackOverflow社区ebox.Text = Trim(DataGridView1.Rows(i).Cells(0).Value)
salarybox.Text = DataGridView1.Rows(i).Cells(1).Value
End If
End If
now if the cell has nothing in it then it will show the exception....
like this...Operator '<>' is not defined for type 'DBNull' and 'Nothing'.
this is code will be called when selected cell will be changed.
i trying to get the values of the selected cell and put that in in one text box.You don't want to use the <>
operator, you should us value IsNot Nothing
to check if it IsNot Nothing
or inversely Is Nothing
to check if a value Is Nothing
.
Also the reason is that there is no comparer for the DBNull
and Nothing
Types so if this is the case you will need to check to see for both. Something like
If value IsNot Nothing AndAlso value <> DBNull.Value Then
''#Do something
End If
Change
If DataGridView1.Rows(i).Cells(0).Value <> Nothing Then
to
If Not DataGridView1.Rows(i).Cells(0).Value Is DBNull.Value Then
精彩评论