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
DataGridViewButtonColumn
cell is clicked. TheDataGridView
’sCell_Clicked
andCellContentClicked
events get fired.I was not able to get the delay of clicking into the
DataGridView
once then having to click again to fire the button. When I clicked on theDataGridView
button cell, theCell_Clicked
event was immediately fired. Changing theDataGridView
’sEditMode
made no difference. The code below simply identifies WHICH cell was clicked from theCell_Clicked
event. If the cell clicked was a button column (1 or 2), then I call a created methodButtonHandler
to handle which button was pressed and to continue on to the correct button method. Hope this helps.