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
事件中获取单元格的值时,情况就是这样。
答案 0 :(得分: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