I've got a DataGridView including a DataGridViewButtonColumn. The user should be able to use the button directly so I set EditMode to EditOnEnter. But, the first click didn't fire the Click event - It seems like the first click selects/focus the row/column?
So I tried to use the CellClick Event:
Private Sub dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellClick
Dim validClick = (e.RowIndex <> -1 And e.ColumnIndex <> -1)
If (TypeOf dgv.Columns(e.ColumnIndex) Is DataGridViewButtonColumn And validClick) Then
dgv.BeginEdit(True)
CType(dgv.EditingControl, Button).PerformClick()
End If
End Sub
But this solution didn't work either. EditingControl always throws a NullReferenceException.
Any ideas?
I do not think there is a specific event available to handle when a
DataGridViewButtonColumncell is clicked. TheDataGridView’sCell_ClickedandCellContentClickedevents get fired.I was not able to get the delay of clicking into the
DataGridViewonce then having to click again to fire the button. When I clicked on theDataGridViewbutton cell, theCell_Clickedevent was immediately fired. Changing theDataGridView’sEditModemade no difference. The code below simply identifies WHICH cell was clicked from theCell_Clickedevent. If the cell clicked was a button column (1 or 2), then I call a created methodButtonHandlerto handle which button was pressed and to continue on to the correct button method. Hope this helps.