Solved

VB.Net  Datagridview. Restricting cell to Numerals only wont work

Posted on 2014-02-02
3
422 Views
Last Modified: 2014-02-04
Hi Experts.   Please could someone explain why this code wont work.  I am trying to restrict column two to numerals only.   It works fine on the first entered line but after editing row 0 column 2 for the first time, thereafter all cells are restricted to numerals.  Any help would be appreciated.   Many Thanks.

Private Sub DataGridView2_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView2.EditingControlShowing

        If DataGridView2.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)

       Select e.KeyChar
            Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", vbBack
                e.Handled = False
            Case Else
                e.Handled = True
        End Select

End Sub
0
Comment
Question by:PNRT
3 Comments
 
LVL 5

Expert Comment

by:NARANTHIRAN
ID: 39828864
Use the following to check the character enter in the data-grid and execute the commands
based on it..

If (Char.IsDigit(e.KeyChar)) Then
        'this is a number
    Else
        'not a number
    End If
0
 
LVL 1

Author Comment

by:PNRT
ID: 39829019
Thanks for the reply but the same problem exists.    The problem is not with the second sub - number or not number.  The problem is in the first sub in that the second sub is called whether column 2 is edited or not.  It goes to the second sub and activates even when column 1 or 2 is edited.   So after the first row is edited correctly, only numerals can be entered into column 1 and 2.   Thanks again for the help
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 39831750
Hi...
Try to Remove the handler initially.Just add one more line
    If DataGridView2.CurrentCell.ColumnIndex = 2 Then
          RemoveHandler Ctype (e.Control, Textbox)..........
          AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
        End If

Open in new window


Yiannis
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now