Solved

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

Posted on 2014-02-02
3
424 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 2

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Calling web service and its methods dynamically 2 59
code conversion from excel vba to visual basic for visual studio 5 49
VS.net 2010 11 34
ASP/VB email question 4 33
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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