3

任何人都可以给我代码示例如何捕获 datagridview 单元格按键事件吗?Datagridview_keypress 没有帮助。

谢谢

4

6 回答 6

6

根据 Fco Navarro 的回答,除了 usinge.Control并不总是有效,因为e传递给EditingControlShowing事件ByVal意味着对控件的任何更改(例如更改.Text属性)都不会反映在 DataGridView 中。如果您需要对事件处理程序中的实际 TextBox 控件执行任何操作,可以DataGridView1.EditingControl使用e.Control.

Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    txtNumeric = CType(DataGridView1.EditingControl, DataGridViewTextBoxEditingControl)
End Sub

Private Sub txtNumeric_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtNumeric.KeyPress
    txtNumeric.Text = txtNumeric.Text.ToUpper()
End Sub
于 2015-02-17T02:21:28.543 回答
2
Private WithEvents txtNumeric As New DataGridViewTextBoxEditingControl

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    txtNumeric = CType(e.Control, DataGridViewTextBoxEditingControl)
End Sub

Private Sub txtNumeric_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNumeric.KeyDown
    If (e.KeyData >= Keys.A And e.KeyData <= Keys.Z) Then
        e.SuppressKeyPress = True
    End If
End Sub
于 2013-09-24T17:20:43.043 回答
1

尝试这个:

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) 处理 DataGridView1.EditingControlShowing

    如果 Me.DataGridView1.CurrentCell.ColumnIndex = 0 而不是 e.Control 什么都不是,那么
        Dim tb As TextBox = CType(e.Control, TextBox)
        AddHandler tb.KeyDown, AddressOf TextBox_KeyDown
        AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
    万一

结束子

Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    如果 e.KeyCode = Keys.Space 那么
        标志 = 真
    万一
结束子

Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    e.Handled = 标志
    标志 = 假
结束子

从这里提取。

于 2013-02-20T11:29:35.100 回答
1

我使用 KeyUp 事件而不是 KeyPress。诀窍是当单元格处于活动状态时,将处理程序事件附加到表单的 ActiveControl 属性。

Private Sub grid_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles grid.CellBeginEdit
    AddHandler Me.ActiveControl.KeyUp, AddressOf Cell_KeyUp
End Sub

Private Sub Cell_KeyUp(sender As Object, e As KeyEventArgs)
    Console.WriteLine(sender.Text) 'content of cell
End Sub

如果您还想使用 RemoveHandler 方法,您可以在类中添加一个全局变量(使用这个变量而不是 Me.ActiveControl),并在 DataGridView 的 CellEndEdit 事件中调用 RemoveHandler,例如。

于 2020-06-06T16:55:43.973 回答
0

如果您在 asp.net(网站)中使用您的 gridview 是不可能的。没有按键事件。确实有可能,但您必须使用 JavaScript 对每个 keychanged(客户端)进行回发。但这不是一种好的编程风格,不应该使用(你会收到很多回发,这会减慢整个系统的速度)。

如果您使用 Windows 表单:请查看 sysdragon 的答案。

希望这个对你有帮助。

最好的问候,没有人

于 2013-02-20T11:33:46.487 回答
0

以下代码完美运行:

Private WithEvents txtmontant As DataGridViewTextBoxEditingControl
Private Sub DGdivers_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DGdivers.EditingControlShowing
    If DGdivers.CurrentCell.ColumnIndex = 1 Then
        Dim txtmontant = CType(e.Control, DataGridViewTextBoxEditingControl)
        AddHandler txtmontant.KeyPress, AddressOf txtmontant_keypress
    Else
        RemoveHandler txtmontant.KeyPress, AddressOf txtmontant_keypress
    End If

End Sub

Private Sub txtmontant_keypress(sender As Object, e As KeyPressEventArgs) Handles txtmontant.KeyPress
    If e.KeyChar = vbCr Then
        DGdivers.Rows.Add()
        Exit Sub
    End If
    If e.KeyChar = vbBack Then
        Exit Sub
    End If
    If InStr("0123456789.,", e.KeyChar) = 0 Then
        e.KeyChar = ""
    End If
End Sub
于 2017-03-19T19:39:22.247 回答