How to Validate A Grid Click Event when the user does not click on an active row?

tcl4p

Well-known member
Joined
Feb 29, 2008
Messages
48
Programming Experience
1-3
Maybe I'm approaching this the wrong way, but what I'm trying to do is to do is to validate a click event on a grid control. In testing the application the code below works fine, when, in fact, you click on a row on the grid, which contains data. That said, when I'm testing for errors and click on "blank" space, I get the following error: Null Reference Exception was unhandled. I've tried using both the IsDbNull and IsNothing functions, but they don't seem to work even though when I check the value returned it shows as Nothing. So the question is 1. Am I using the wrong function to get the value from a cell in the row and 2. How do I add code to make sure if the user clicks in the blank space (not on a row), I can catch the error?
Thanks,
Tom

Private Sub grdCharges_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdCharges.Click
If Me.grdCharges.SelectedRows.Count > 0 Then
If Not IsNothing(grdCharges.Rows(grdCharges.CurrentRow.Index).Cells(0).Value) Then if Not IsDBNull(grdCharges.Rows(grdCharges.CurrentRow.Index).Cells(0).Value) Then
ChargeID = grdCharges.Rows(grdCharges.CurrentRow.Index).Cells(0).Value
End If
End If
End If
End Sub
 
The Click event of a (presumably) DataGridView should rarely be handled. If you want to execute code only when the user clicks on a cell then it would make more sense to handle the CellClick event.
 
Thanks for the response. I'm using the click event because I have the selectionmode property set to FullRowSelect, so the user is selecting a row and I want to pull a value, in this case the ID field and use that value. In this case the ID cell has its visible property set to false. So is the answer to look for another grid event to use?
 
I actually found the problem that's causing the exception, but can't find a way around it. The exception occurs when the user clicks on the row, which in turn takes the user to another form. The original form however is not closed and when the user comes back to the form I use the code below to select the row. Interestingly while the row is selected, if you now click on the blank space I get the error, BUT if I click on the row, then click on the blank space I do not get the error. It would seem that there is some difference in selecting the row in code and actually clicking on the row. What the hell that difference is escapes me and I still can't seem to find a way around the error other then to add a try catch statement an ignore the error assuming the error is coming from a user clicking on blank space. I realize it may be unusual for a user to click on the area not being used to display rows, but it can happen.
Tom


Private Sub frmDefendantHx_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
LoadCharges()

Dim cVal As Integer

For i As Integer = 0 To grdCharges.Rows.Count - 1
cVal = grdCharges.Rows(i).Cells("ID").Value
If cVal = ChargeID Then
grdCharges.Rows(i).Selected = True
End If
Next

End Sub
 
Back
Top