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

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.

SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
  • 1
    @varocarbas calm down buddy.. http://stackoverflow.com/q/19855943/2064292 sorry I should have given this earlier. – SamuraiJack Nov 11 '13 at 09:39
  • It is unclear what you are doing with the textBox/DataGridView. Can you please explain your exact structure, why are you adding the KeyPress event as you are doing it, inside the DataGridView (in principle, not making too much sense)? – varocarbas Nov 11 '13 at 09:39
  • Calm down?! Why are you saying that? Because of the not making any sense bit? You are adding a KeyPress event of a textbox inside a datagridview, this is really weird. But will test it (now with more information) and will correct my statement if required, but I was very calmed anyway. – varocarbas Nov 11 '13 at 09:39
  • Ah! I see! (yes, you should provide all your code; mainly when it is a bit unorthodox) Satson provided you this "peculiar approach". Never have seem such a thing (and not sure about the exact point). Will test it right now. – varocarbas Nov 11 '13 at 09:41
  • Did you Check my answer? – Sathish Nov 11 '13 at 09:50
  • Yes, I was doing it right now. Honestly, I never do these kind of things (usually rely on the `ValueChange` event; and affect the cell while being edited only under extreme conditions); also I don't like too much adding so many events. But it seems to work fine, at least, under the conditions I tested it. I think that this might provoke problems when intending to coordinate cells (column/row) with textboxes, also not too sure about its reliability under more demanding conditions (big number of cells, multiple accesses, variable DataSource, etc.), but if the OP is happy I guess that it is OK – varocarbas Nov 11 '13 at 09:53

1 Answers1

2

Try this Code

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
varocarbas
  • 12,354
  • 4
  • 26
  • 37
Sathish
  • 4,419
  • 4
  • 30
  • 59
  • Why are you suggesting something without even understanding what the OP wants/what is going on? Are you aware that the TextBox_keyPress method you refer is attached to the KeyPress event of the TextBox wrongly and thus this function will never be called (not when pressing a key in the TextBox)? – varocarbas Nov 11 '13 at 09:25
  • Well.. unless, it has included a textbox in the DataGridView (why?). But lots of explanations are required to understand what is going on and you cannot solve something you don't even know what is doing – varocarbas Nov 11 '13 at 09:31
  • @varocarbas Now Check my answer. Its Correct – Sathish Nov 11 '13 at 09:31
  • All Clear, next time, intend to be a bit more descriptive (e.g., I know what is going on; I answered the original question, etc.). You should convert your code into: `If Char.IsDigit(e.KeyChar) = False Then e.Handled = True;` and Perform a `DirectCast` of the `sender` to a `TextBox`; other than that, it should be fine. – varocarbas Nov 11 '13 at 09:55
  • No problem but, as said, you should remove CChar(Cstr it is redundant (you take a char, convert it to string and then back to char). Also perhaps you should use .NET equivalents for next time (Convert.ToChar, ToString or Convert.ToString; and DirectCast) – varocarbas Nov 11 '13 at 09:59
  • I have not been able to test this code yet.. I will respond as soon as I do. – SamuraiJack Nov 11 '13 at 10:03
  • @Arbaaz This answer delivers what you want and you should accept it. But bear in mind that this approach might become too problematic (perhaps even on realiability fronts) mainly when you have to relate the given textbox to a cell (column/row indices) as far as they are completely different things. Perhaps you should take a look at more reliable approaches, not affecting the cell while is being edited, but when the user inputs the given value (example: the user can type letters but, once he presses enter or chooses a different cell, all the letters are removed). – varocarbas Nov 11 '13 at 10:11