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

    If DataGridView1.CurrentCell.ColumnIndex = 2 Then

        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress

    End If

End Sub

Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)

    If Char.IsDigit(CChar(CStr(e.KeyChar))) = False Then e.Handled = True

Dim str As String =  DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(DataGridView1.CurrentCell.ColumnIndex).Value 
'null reference error here
End Sub

我尝试了很多东西,但它总是给我空引用错误。当我尝试在EditingControlShowing 事件中获取单元格的值时,情况也是如此。

4

1 回答 1

2

试试这个代码

Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)

    If Char.IsDigit(e.KeyChar) = False Then
        e.Handled = True
    Else
        Dim str As String = CType(sender, TextBox).Text
        'Do stuff

    End If

End Sub
于 2013-11-11T09:20:00.827 回答