)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to make the grid cell ignore all keys pressed, except numbers in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: December 18, 2019
Tags: keyhandling

CurrentCellKeyPress event

This can be achieved by handling the CurrentCellKeyPress event of the GridControl and when the key is not a digit, then set the e.Handled to “True”.

C#

//set the cells of the 3rd column into integer type.
this.gridControl1.ColStyles[2].CellValueType = typeof(int);
//using  CurrentCellKeyPress
void gridControl1_CurrentCellKeyPress(object sender, KeyPressEventArgs e)
{
    GridCurrentCell cc = this.gridControl1.CurrentCell;
    //condition for ignore key other than numbers
    if (this.gridControl1[cc.RowIndex, cc.ColIndex].CellValueType == typeof(int) && !char.IsDigit(e.KeyChar))
    {
         e.Handled = true; //ignore it
        MessageBox.Show("Invalid entry");
    }
}

VB

'set the cells 3rd column into integer type
Me.gridControl1.ColStyles[2].CellValueType = GetType(Integer)
'using  CurrentCellKeyPress
Private Sub gridControl1_CurrentCellKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
    Dim cc As GridCurrentCell = Me.gridControl1.CurrentCell
    'condition for ignore key other than numbers
    If Me.gridControl1(cc.RowIndex, cc.ColIndex).CellValueType Is GetType(Integer) AndAlso (Not Char.IsDigit(e.KeyChar)) Then
        e.Handled = True 'ignore it
        MessageBox.Show("Invalid entry")
    End If
End Sub

Samples:

C#: IgnoreKeys

VB: IgnoreKeys

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon