Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8627173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:12:39+00:00 2026-06-12T08:12:39+00:00

I want to only allow the user to enter 0..9, . and backspace in

  • 0

I want to only allow the user to enter 0..9, “.” and backspace in my DGV. I thought the KeyDown event might be the ticket, with something like this:

private void dataGridViewPlatypi_KeyDown(object sender, KeyEventArgs args)
{
    args.Handled = !args.KeyCode.Equals(Keys.Decimal); // etc. - add other allowed vals
    if (args.Handled)
    {
        args.SuppressKeyPress = true;
    }
}

…but that doesn’t work/has no effect.

I researched and found this: DataGridView keydown event not working in C#

…which led me to creating a custom DGV class with this:

protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
    if ((!keyData.Equals(Keys.Decimal)) &&
        (!keyData.Equals(Keys.???))) // etc.
    {
        //suppress the key somehow
    }
    return base.ProcessCmdKey(msg, keyData); // <-- should this be in an "else" block?
}

…but as you can tell from the “???” and the “somehow” comment, I don’t know how to test for the other keys I want to allow (0..9 and backspace)

UPDATE

With able assistance from two respondents, it’s now working just great:

I started off with Hans Passant’s code here to create a custom DGV-derived control to solve the moveable final row problem:

removing the empty gray space in datagrid in c#

…and then added code based on LarsTech’s to filter out unwanted entries in the DGV.

To be precise, this is the exact logic I used to override ProcessCmdKey():

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (char.IsNumber(Convert.ToChar(keyData)) || 
        char.IsControl(Convert.ToChar(keyData)) || 
        (keyData >= Keys.NumPad0 && keyData <= Keys.NumPad9) ||
        (keyData == Keys.Space) ||
        (keyData == Keys.Back) ||
        (keyData == Keys.Decimal))
    {
        return false;
    }
    return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:12:40+00:00Added an answer on June 12, 2026 at 8:12 am

    Handling the keys of a text input control is almost never 100%. You might want to investigate using a MaskedTextBox control in the DataGridView in this Code Project.

    For the override, this code will get you close:

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
      if (
        (keyData >= Keys.D0 && keyData <= Keys.D9) ||
        (keyData >= Keys.NumPad0 && keyData <= Keys.NumPad9) ||
        (keyData == Keys.Decimal | keyData == Keys.OemPeriod) ||
        (keyData == Keys.Back | keyData == Keys.Delete) ||
        (keyData == Keys.Left | keyData == Keys.Up | keyData == Keys.Right | keyData == Keys.Down) ||
        (keyData == Keys.Tab | keyData == Keys.Home | keyData == Keys.End | keyData == Keys.Enter)
        ) {
        return false;
      }
    
      return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to allow user to enter only english alphabets and characters. How can
I want to allow a user to drag my Win32 window around only inside
I have made this batch script which will allow the user to enter a
In my web application, I want the user to enter only SELECT statements. I
I want to only allow users with IE8 (not IE6, IE7) or another browser
I have a two UIViews filled with UIButtons. I want to only allow one
I want to allow only English characters to be typed in my asp:TextBox (in
I want to extend Ion Auth to only allow certain email addresses to register.
What should I do if I want to allow only English alphabet to be
I have a wordpress site that I only want to allow people to post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.