本文介绍了如何在EditingControlShowing事件中找到DataGridViewTextBox单元格的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 事件中获取单元格的值时,也是如此。

I tried a number of things but it always gives me null reference error.It's the same story when I try to get the value of the cell in EditingControlShowing event.

推荐答案

尝试此代码

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

这篇关于如何在EditingControlShowing事件中找到DataGridViewTextBox单元格的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 03:22