Swim89
Swim89

Reputation: 310

VB.NET Click a checkbox into Datagridview

I have a DatagridView with data directly from a recordset.

I had a new column before the others as a checkbox type with this code:

Dim chk As New DataGridViewCheckBoxColumn()
gridRicette.Columns.Add(chk)
chk.HeaderText = "Sel."
chk.Name = "chk"

And it appear. But when I click on the the checkbox, nothing happens.

Can you help me to solve it?

Thanks!

Upvotes: 0

Views: 4752

Answers (1)

User9995555
User9995555

Reputation: 1556

Try something like this....

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim chk As New DataGridViewCheckBoxColumn()
    gridRicette.Columns.Add(chk)
    chk.HeaderText = "Sel."
    chk.Name = "chk"
End Sub

Private Sub gridRicette_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles gridRicette.CellContentClick
    Dim senderGrid As DataGridView = sender
    Dim data = senderGrid.Rows(e.RowIndex).DataBoundItem

    If senderGrid.Columns(e.ColumnIndex).GetType() Is GetType(DataGridViewCheckBoxColumn) And e.RowIndex >= 0 Then
        MessageBox.Show(String.Format("You selected row {0}", e.RowIndex))
    End If
End Sub
End Class

You can use the DataGridView1_CellContentClick event and then determine what has been clicked by the user.... hope that helps.

Upvotes: 1

Related Questions