Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to prevent Non-Numeric Input in DataGridView ?
Message
From
16/10/2007 23:39:14
Handi Rusli
PT. Alam Sumbervita
Jakarta, Indonesia
 
 
To
16/10/2007 23:25:42
Handi Rusli
PT. Alam Sumbervita
Jakarta, Indonesia
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
01261399
Message ID:
01261401
Views:
8
>Hi all,
>
>I want to prevent non-numeric input from the first user's keystroke in DataGridView (mimic behaviour of Numeric TextBox in VFP).
>
>Anybody know how to do it ?
>
>Thanks in advance :-D

I've got the answer in VB.NET Language from :

http://www.devx.com/dotnet/Article/33748/1954?pf=true

Anybody can translate it to C# code ? Thanks :-D
Restricting Inputs
The previous section shows how you can validate the value for a cell after the user has finished typing it.
However, in some situations this is not sufficient. A better way would be to prevent illegal characters from being entered in the first place.
Using the same example from the previous section, you should prevent users from entering non-numeric characters in the Price field.
For normal TextBox controls, this problem could be solved by servicing the KeyPress event.
However, applying this technique to the DataGridView control requires some additional work.
First, service the EditingControlShowing event. This event is fired when the user tries to edit the content of a cell: 


    Private Sub DataGridView1_EditingControlShowing( _
       ByVal sender As Object, _
       ByVal e As System.Windows.Forms. _
       DataGridViewEditingControlShowingEventArgs) _
       Handles DataGridView1.EditingControlShowing

        '---restrict inputs on the fourth field---
        If Me.DataGridView1.CurrentCell.ColumnIndex = 3 And _
           Not e.Control Is Nothing Then
            Dim tb As TextBox = CType(e.Control, TextBox)

            '---add an event handler to the TextBox control---
            AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
        End If
    End Sub

Here, you will add a KeyPress event handler to the TextBox control that you want to restrict. This KeyPress event handler will be invoked when the user types into the cell and it is defined as follows: 

    Private Sub TextBox_KeyPress( _
       ByVal sender As System.Object, _
       ByVal e As System.Windows.Forms.KeyPressEventArgs)

        '---if textbox is empty and user pressed a decimal char---
        If CType(sender, TextBox).Text = String.Empty And _
           e.KeyChar = Chr(46) Then
            e.Handled = True
            Return
        End If
        '---if textbox already has a decimal point---
        If CType(sender, TextBox).Text.Contains(Chr(46)) And _
           e.KeyChar = Chr(46) Then
            e.Handled = True
            Return
        End If
        '---if the key pressed is not a valid decimal number---
        If (Not (Char.IsDigit(e.KeyChar) Or _
           Char.IsControl(e.KeyChar) Or _
           (e.KeyChar = Chr(46)))) Then
            e.Handled = True
        End If
    End Sub
Handi Rusli
"Treat people with respect and courtesy, and one will be blessed. Complete task heartily, with best effort and responsibility, and one can achieve the greatest."
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform