Search code examples
vb.netdatagridviewdatagridviewcolumn

column specific click event in datagridview vb.net


I have a dataGridView that has 2 columns called (colour & desc) and I want to have a click event that only fires on the (colour) column.

I have searched a bit on the net and seen various ways to do this via row index but nothing by column index.

Any help would be appreciated..,thank you


Solution

  • Create event handler for datagridview.CellClick event

    Private Sub dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellClick
        Dim yourColumnIndex As Int32 = 1
        If e.ColumnIndex = yourColumnIndex Then
            'Do your staff
        End If
    End Sub
    

    Or if you have predefined columns then:

    Private Sub dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellClick
        If e.ColumnIndex = Me.colour.Index Then
            'Do your staff
        End If
    End Sub