![](/img/trans.png)
[英]Button.Click event doesn't fire if datagridview is in editmode
[英]Manually fire button click event in DataGridView
我有一个DataGridView
包括DataGridViewButtonColumn
。 用户应该可以直接使用按钮,因此我将EditMode
设置为EditOnEnter
。 但是,第一次单击没有触发Click
事件-似乎第一次单击选择了/重点放在了行/列上?
因此,我尝试使用CellClick
事件:
Private Sub dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellClick
Dim validClick = (e.RowIndex <> -1 And e.ColumnIndex <> -1)
If (TypeOf dgv.Columns(e.ColumnIndex) Is DataGridViewButtonColumn And validClick) Then
dgv.BeginEdit(True)
CType(dgv.EditingControl, Button).PerformClick()
End If
End Sub
但是此解决方案也不起作用。 EditingControl
总是抛出NullReferenceException
。
有任何想法吗?
我不认为单击DataGridViewButtonColumn
单元格时可以处理特定事件。 触发DataGridView
的Cell_Clicked
和CellContentClicked
事件。
我无法获得一次单击进入DataGridView
的延迟,然后不得不再次单击以触发该按钮。 当我单击DataGridView
按钮单元格时,立即触发了Cell_Clicked
事件。 更改DataGridView
的EditMode
没什么区别。 下面的代码仅标识WHICH单元是从Cell_Clicked
事件中单击的。 如果单击的单元格是一个按钮列(1或2),那么我将调用一个创建的方法ButtonHandler
来处理按下了哪个按钮,然后继续使用正确的按钮方法。 希望这可以帮助。
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
if (e.ColumnIndex == 1 || e.ColumnIndex == 2) {
// one of the button columns was clicked
ButtonHandler(sender, e);
}
}
private void ButtonHandler(object sender, DataGridViewCellEventArgs e) {
if (e.ColumnIndex == 1) {
MessageBox.Show("Column 1 button clicked at row: " + e.RowIndex + " Col: " + e.ColumnIndex + " clicked");
// call method to handle column 1 button clicked
// MethodToHandleCol1ButtonClicked(e.RowIndex);
}
else {
MessageBox.Show("Column 2 button clicked at row: " + e.RowIndex + " Col: " + e.ColumnIndex + " clicked");
// call method to handle column 2 button clicked
// MethodToHandleCol2ButtonClicked(e.RowIndex);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.