I have only recently began using the DataGridView and I find it quirky in many aspects. For example when attempting to read the value of a cell, if that cell has no value or is purposefully set to NULL, a System.NullReferenceException is thrown .. and to be clear, the DataGridView and all columns are unbound.
For my current needs, I will frequently have NULL values in cells, some by default and some by choice.
The form has 2 DataGridViews, one with 2 columns and one with 1 column.
The way this works is the user selects a cell to add data, however, if they select the second column "column(1)" in DataGridView1, DataGridView2 shows bound data that the user then selects from to fill the cell, sort of like a having a linked table in the database. The data in DataGridView2 is retrieved based upon the value entered in Rows(X).Cells(0) of DataGridView1 (i.e. the cell in the first column of the corresponding row). If this value is NULL, a default table is used to fill DatGridView2.
Currently, I have the following bit of code to handle the problem, but I'd rather not have the exception. Is this the best way to handle these? I'd appreciate any input.
For my current needs, I will frequently have NULL values in cells, some by default and some by choice.
The form has 2 DataGridViews, one with 2 columns and one with 1 column.
The way this works is the user selects a cell to add data, however, if they select the second column "column(1)" in DataGridView1, DataGridView2 shows bound data that the user then selects from to fill the cell, sort of like a having a linked table in the database. The data in DataGridView2 is retrieved based upon the value entered in Rows(X).Cells(0) of DataGridView1 (i.e. the cell in the first column of the corresponding row). If this value is NULL, a default table is used to fill DatGridView2.
Currently, I have the following bit of code to handle the problem, but I'd rather not have the exception. Is this the best way to handle these? I'd appreciate any input.
VB.NET:
Private Sub DataGridView1_CellClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellClick
If e.ColumnIndex = 1 Then
Dim strValue As String
Try
strValue = sender.Rows(e.RowIndex).Cells(0).Value.ToString
Catch ex As Exception
strValue = "Default"
End Try
End If
LoadDataTable(Me.DataGridView2, strValue)
End Sub