3

I am a tad new to the DataGrid controls, but I am just curious as to why the first code block below works, but the second code block does not? (Only thing I can see is the Handles DataGridClaims syntax

Block 1

Private Sub DataGridClaims_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridClaims.CellContentClick
    If e.RowIndex <> -1 Then
        Dim frmViewClaims As New objViewClaim
        frmViewClaims.ClaimID = DataGridViewClaims.CurrentRow.Cells("ClaimNum").Value
        frmViewClaims.Show()
    End If
End Sub

Block 2

Private Sub DataGridClaims_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
    If e.RowIndex <> -1 Then
        Dim frmViewClaims As New objViewClaim
        frmViewClaims.ClaimID = DataGridViewClaims.CurrentRow.Cells("ClaimNum").Value
        frmViewClaims.Show()
    End If
End Sub
user279521
  • 4,779
  • 21
  • 78
  • 109

2 Answers2

2

I am not too familiar with the VB.NET, but CellContentClick is an event which occurs when the content within a cell is clicked.

In order for the program to understand that this is an event you use the keyword Handles in VB.NET. It allows you to wire-up the bindings to event handlers on the event handler methods themselves.

This is the equivalent of += in c# and would look something like

DataGridClaims.CellContentClick += DataGridClaims_CellContentClick;

VoodooChild
  • 9,776
  • 8
  • 66
  • 99
2

The "handles" keyword in VB.net marks the Function as a listener to the given event. Without the "Handles DataGridClaims", the grid has no way to know to fire your function when the event is triggered.

[See MSDN Doc's][1] http://msdn.microsoft.com/en-us/library/6k46st1y(v=VS.100).aspx