Solved

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

Posted on 2014-02-02
3
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

763 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