如何在EditingControlShowing事件中找到DataGridView TextBox Cell的值?

时间:2013-11-11 05:28:11

标签: .net vb.net datagridview

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事件中获取单元格的值时,情况就是这样。

1 个答案:

答案 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