I have a VB project I’m working on where I have to break the GridView down to each row and examine a specific cell. How do I go about checking to see if there is a null value or not? The following code results in the error message
“Specified argument was out of the range of valid values.
Parameter name: index”
I’ve checked the cell count for the GridViewRow variable “row” and it comes up at 5, so I’m not sure what I’m doing wrong.
Protected Sub grdProduct_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdProduct.RowDataBound
' Grey out expired products
Dim row As GridViewRow
row = e.Row
Dim incomingStatus As String
If row.Cells(5).Text.ToString() <> vbNull Then
incomingStatus = row.Cells(5).Text.ToString()
Else
incomingStatus = ""
End If
I modified the code to check the cell count first and it is still coming back with the exact same error.
If row.Cells.Count > 5 And row.Cells(5).Text.ToString() <> vbNull Then
incomingStatus = row.Cells(5).Text.ToString()
Else
incomingStatus = ""
End If
FINAL EDIT
Modifying the code like so fixed the problem. Thanks guys:
If row.Cells.Count > 5 Then
If row.Cells(5).Text.ToString() <> vbNull Then
incomingStatus = row.Cells(5).Text.ToString()
Else
incomingStatus = ""
End If
End If
row.Cells(5)returns the 6th Cell, not the 5th since indices are 0 based in .NET. But theCountproperty returns the actual number of cells.The same is true for
GridView.Rows.returns the 10th
GridViewRow.