column specific click event in datagridview vb.net

susiana picture susiana · Jun 20, 2014 · Viewed 8k times · Source

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

Answer

Fabio picture Fabio · Jun 20, 2014

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