Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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 …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

810 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