0

当我在表单上有许多控件(即标签、按钮等)做几乎相同的事情时,我经常使用一种方法来处理所有控件单击,

但是要知道哪些控件引发了事件并访问该控件的属性,我需要将“发送者”对象转换为正确的类型。

在数据网格视图中:

我想从datagridview单元格中的按钮获取文本我尝试了这个,但它不是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
4

1 回答 1

1
Dim button As DataGridViewButtonCell = DirectCast(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewButtonCell)
MessageBox.Show(button.Value)
于 2012-07-04T15:49:58.780 回答