Help Man Help Man - 1 year ago 168 Question

Add cell click event handler to datagridview?

I have three columns, first column is textbox, second column is checkbox, third column is textbox. I want to add a click event to the third column where if a user click on that cell, it will automatically checkmark and uncheckmark the second checkbox column of that row. I tried this but it's not working.

AddHandler datagridview1.MouseClick, AddressOf form1.datagridview1_MouseClick

Answer Source

just need to switch your Handle type on the subroutine to "Handles DataGridView1.CellClick". Example:

 Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    Dim tempView = DirectCast(sender, DataGridView)

    For Each cell As DataGridViewTextBoxCell In tempView.SelectedCells
        If cell.ColumnIndex = 1 Then
            Dim tempCheckBoxCell As DataGridViewCheckBoxCell = tempView("column1", cell.RowIndex)
            tempCheckBoxCell.Value = True
        End If
End Sub

Also, quick note - you will need to adjust the cell type found in the for each loop to whatever type of cell you are using; in the example I had column2 set to a simple textbox type cell.