?
Solved

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

Posted on 2014-02-02
3
Medium Priority
?
429 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 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