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 3932656
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:30:51+00:00 2026-05-19T23:30:51+00:00

I have a datagridview on the form. and there are 4 columns in the

  • 0

I have a datagridview on the form.
and there are 4 columns in the grid – No, Answer, Key, Result.
No, Key and Result Columns are read only.
it means that i’m not going to enter some data to these columns.
But i will enter values to Answer column.
And values can be only – A, B, C, D and E.
when i press for example X key i dont want X to be show in the cell.
i can do it with textbox’s with keypress event.

now can you tell me, how can i use keypress event of Answer columns cell event to realize it.

  • 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-05-19T23:30:51+00:00Added an answer on May 19, 2026 at 11:30 pm

    You can use the EditingControlShowing event of the DataGridView to accomplish this.

    In the eventhandler of this event, you have access to the Textbox that ‘s being displayed whenever you enter data inside the datagrid.
    This means, that at this point, you can attach an eventhandler to the KeyPress event of the textbox that is displayed:

            private bool _firstTime = true;
    
            private void dataGridView1_EditingControlShowing( object sender, DataGridViewEditingControlShowingEventArgs e )
            {
                if( !_firstTime )
                {
                    return;
                }
    
                _firstTime = false;
    
                var t = e.Control as TextBox;
    
                if( t != null )
                {
                    t.KeyPress += OnKeyPress;
                }
            }
    
            private void OnKeyPress( object sender, KeyPressEventArgs e )
            {
                if( e.KeyChar != 'A' && e.KeyChar != 'B' && e.KeyChar != 'C' )
                {
                    e.Handled = true;
                }
            }
    

    Since the DataGridView will always ‘share’ the textbox control for every cell in the grid that uses textboxes, you should check whether it is the first time that the event is raised.
    If you have other columns in the DataGridView which are not readonly, and where you want the user to enter data as well, which is not constrained, then this approach won’t be that suitable. (Unless you check in the OnKeyPress eventhandler to which column the current cell belongs).

    (Note that you’ll have to consider the lowercase a, b, c as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that contains dataGridView, whose coloumn are set to dgrv1.Width =dgrv1.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)+20;
I have a form created which an unbound datagridview that has three columns. Position,
I have a DataGridView in a windows form. I defined 5 columns for this
I have a datagridview on a form with multiple columns. Three of the columns
Is there a simple way to re-ordering the columns of a DataGridView that are
I have a DataGridView in C# (.NET 2.0) with a few read-only cells (3
I have a datagridview that is linked to a three columns in the database,
I have a DataGridView in a form that has a data table as the
I have a DataGridView with read-only mode set to true (non-editable) which takes values
I have a form with a datagridview and a button. There will be more

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.