When I have many controls on a Form (i.e. Label, Button etc) that do almost the same thing, I often use one method to handle all the controls Click,
But to know which of the controls throwing the event and access the properties of that control I need to cast the “sender” object to the correct type.
in datagridview :
i want to get the text from a button in a cell in datagridview
i try this but it’s not wotking :s :s
Dim btnGrid As New DataGridViewButtonColumn
btnGrid.HeaderText = "Modifier les lieu"
btnGrid.Text = "Mise a jour"
btnGrid.UseColumnTextForButtonValue = True
DataGridView1.Columns.Add(btnGrid)
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex = 0 Then
index = DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value
Dim btn As Button = CType(sender, DataGridViewButtonColumn)
MsgBox(btn.Text)
End If
End Sub
1 Answer