DatagridView 事件仅允许数字、退格键和删除键 VB.Net

DatagridView Event To allow Numbers, Backspace & Delete Keys Only VB.Net

如何更改以下代码以接受 DeleteBackSpace 键??

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    Select Case DataGridView1.CurrentCell.ColumnIndex
        Case Is = 0, 1
            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
    End Select

End Sub

Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then
        e.Handled = True
    End If
End Sub

修改后的当前代码 - 不工作

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    Select Case DataGridView1.CurrentCell.ColumnIndex
        Case Is = 0, 1
            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyDown
    End Select

End Sub

Private Sub TextBox_keyDown(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then
        e.Handled = True
    End If
End Sub

编辑 - 2 代码

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

        Select Case DataGridView1.CurrentCell.ColumnIndex
            Case Is = 0, 1
                AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyDown
        End Select

    End Sub

    Private Sub TextBox_keyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        If Not (Char.IsDigit(CChar(CStr(e.KeyValue))) Or e.KeyValue = ".") Then
            e.Handled = True
        End If
    End Sub

现在 TextBox_keyDown Word 这一行出现错误...

AddressOf TextBox_keyDown

错误文本

Severity Code Description Project File Line Suppression State Error BC31143 Method 'Private Sub TextBox_keyDown(sender As Object, e As KeyEventArgs)' does not have a signature compatible with delegate 'Delegate Sub KeyPressEventHandler(sender As Object, e As KeyPressEventArgs)'.

将 DataGridview EditMode 属性 更改为 EditOnEnter 并使用以下代码。

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    Select Case DataGridView1.CurrentCell.ColumnIndex
        Case Is = 0, 1
            AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
    End Select

End Sub

Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    If e.KeyChar <> ControlChars.Back Then
        e.Handled = Not (Char.IsDigit(e.KeyChar))
    End If
End Sub

这将只允许数据网格视图的第 1 列中的数字、退格键、箭头键、删除、主页、结束、空格键。

private void DGV_Test_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
            if (DGV_Test.CurrentCell.ColumnIndex == 0) //Desired Column
            {
                TextBox tb = e.Control as TextBox;
                if (tb != null)
                {
                    tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress);                    
                }
            }
        }

        private void Column1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar))
            {
                if (e.KeyChar == 0x20)  // Allow space also
                {

                }
                else
                {
                    e.Handled = true;
                }
            }
        }