[英]IF Button in datagridview is clicked vb.net
I want to click button in datagridview by code.Can you help me? 我想按代码单击datagridview中的按钮。能帮我吗?
Dim btn1 As New DataGridViewButtonColumn()
Data_analysis_confirm.Columns.Add(btn1)
btn1.HeaderText = "แก้ไข"
btn1.Text = "เลือก"
btn1.Name = "btn_edit"
btn1.UseColumnTextForButtonValue = True
I'll try this code and it not work THIS CODE 我将尝试此代码,但此代码不起作用
Private Sub Data_analysis_confirm_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles Data_analysis_confirm.CellClick
If Data_analysis_confirm.Columns(e.ColumnIndex).Name = "แก้ไข" Then
MsgBox("แก้ไข")
End If
End Sub
I would suggest to use handle the CellContentClick event instead, which fires only when content in a cell is clicked. 我建议改用处理CellContentClick事件,该事件仅在单击单元格中的内容时才会触发。 The CellClick event will fire when any part of a cell is clicked.
单击单元格的任何部分时,将触发CellClick事件。
Additionally your code has an issue where you are comparing the wrong value for the column name ( แก้ไข
instead of the actual name, btn_edit
). 此外,您的代码还有一个问题,您正在比较列名的错误值(用
แก้ไข
代替实际名称btn_edit
)。
Private Sub Data_analysis_confirm_CellContentClick(sender As System.Object, e As DataGridViewCellEventArgs) Handles Data_analysis_confirm.CellContentClick
If e.RowIndex < 0 Then
Exit Sub
End If
Dim grid = DirectCast(sender, DataGridView)
If TypeOf grid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn Then
If grid.Columns(e.ColumnIndex).Name = "btn_edit" Then
MsgBox("แก้ไข")
End If
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.