Learn how to a build a cloud-first strategyRegister Now


How to accept only numbers in VS2008 Datagridview cell for windows application ?

Posted on 2009-02-17
Medium Priority
Last Modified: 2013-11-26
I'm using Visual Studio 2008 DatagridView.
Question by:Jose Bredariol
LVL 53

Assisted Solution

Dhaest earned 260 total points
ID: 23660351
Restricting numeric entries only in a DataGridView column

Author Comment

by:Jose Bredariol
ID: 23662280
I tried this code, but I think this is for C#. I need something similar to VS2008 (VBA). Thanks
LVL 53

Assisted Solution

Dhaest earned 260 total points
ID: 23662495
A quick translation from c# to vb.net
(I didn't have time to test it)
Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) 
    If CInt((DirectCast((sender), System.Windows.Forms.DataGridView).CurrentCell.ColumnIndex)) = 1 Then 
        AddHandler e.Control.KeyPress, AddressOf Me.TextboxNumeric_KeyPress 
    End If 
End Sub 
Private Sub TextboxNumeric_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) 
    Dim nonNumberEntered As Boolean 
    nonNumberEntered = True 
    If (e.KeyChar >= 48 AndAlso e.KeyChar <= 57) OrElse e.KeyChar = 8 Then 
        nonNumberEntered = False 
    End If 
    If nonNumberEntered = True Then 
        ' Stop the character from being entered into the control since it is non-numerical. 
        e.Handled = True 
        e.Handled = False 
    End If 
End Sub 

Open in new window

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

LVL 41

Accepted Solution

graye earned 240 total points
ID: 23662790
Here is a link to an article that has a downloadable VB.Net example for a "masked edit" version of the DataGrid Text box...  It will allow you to specify numbers (or a combination of characaters, like "(###) ###-####")

Author Closing Comment

by:Jose Bredariol
ID: 31547798
Both answers works for me. Thanks

Expert Comment

ID: 23670557
You could just use the RegularExpressionValidator with the proper regex string to allow numbers only.  Try placing the following code within the <EditItemTemplate></EditItemTemplate> for the field you want to control.

Check out http://regexlib.com/Search.aspx?k=numbers%20only for more regular expressions to use.  This is where I got the one for allows numbers only.  Good luck.
<asp:RegularExpressionValidator id="reNumbersOnly" runat="server" ControlToValidate="txtFieldToControl" ValidationExpression="^[+]?\d*$" Text="Numbers Only Please." />

Open in new window


Author Comment

by:Jose Bredariol
ID: 23670751
I'll try this too. Thanks

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

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