vb.net 点击datagridview中的IF按钮vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31579966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IF Button in datagridview is clicked vb.net
提问by Patipat Chewprecha
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
回答by Saragis
I would suggest to use handle the CellContentClickevent instead, which fires only when content in a cell is clicked. The CellClickevent will fire when any part of a cell is clicked.
我建议改用处理CellContentClick事件,该事件仅在单击单元格中的内容时触发。该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

