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

  • Home
  • SEARCH
  • 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 8999855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:14:07+00:00 2026-06-16T00:14:07+00:00

I have a problem with some code I developed based on the article DataGridView

  • 0

I have a problem with some code I developed based on the article DataGridView keydown event not working in C#.

I wanted to allow the user to add a row to a Dataviewgrid control, but found that by enabling AllowUserToAddRows this caused an additional row to be shown as soon as the first character was typed into the new cell in the new row. This would have been confusing to my poor user, so to prevent this, I used the code from above article to immediately disable AllowUserToAddRows at every keystroke (although would have preferred to only do it after the first char was typed). However, this seems to swallow the 1st char typed, i,.e. it is not passed onto the base class for processing. Here’s the full code:

public sealed class XDataGridView : DataGridView
{
    private bool _singleUpdateOnly = true;

    public bool SingleUpdateOnly
    {
        get { return _singleUpdateOnly; }
        set { _singleUpdateOnly = value; }
    }

    [Description("Disallows user adding rows as soon as a key is struck to ensure no blank row at bottom of grid")]
    protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        if (SingleUpdateOnly)
        {
            AllowUserToAddRows = false;
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
}

Why is the 1st char I type swallowed? How can I prevent this from happening? Is there a better way than what I have coded?

  • 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-16T00:14:07+00:00Added an answer on June 16, 2026 at 12:14 am

    I’ve had very little response to this question on this forum and others. It must be a very difficult problem, or I’m simply expecting too much from the DataGridView control.
    After many hours of trial and error, I have settled on a solution which does not require subclassing. The solution is not exactly what I wanted, i.e. to prevent another another row from being appended to the bottom of the grid once the user begins typing into the newly added row, but it comes close. I use the CellLeave event to turn off AllowUserToAddRows. When the user presses Tab or Enter to enter the data into the 1st cell, this effectively removes the newly added row from the grid leaving the user in the next cell of the newly added (last) row of the grid (without an empty row underneath). Not elegant, but at least mostly functional. Here’s the code:

    private void uxContactEmailAddressesGrd_CellLeave(object sender, DataGridViewCellEventArgs e)
    {
        uxContactEmailAddressesGrd.AllowUserToAddRows = false;
    }
    

    Maybe one day someone else will come across this problem and find a solution more elegant than mine. That’d be great. Make sure you post your solution to this site for others to use.

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

Sidebar

Related Questions

I have a problem with some code and I believe it is because of
I have a problem with some source code regarding OpenSSL and Cpp. For some
I have problem to add jQuery to some existing code, please help. We have
I have some problem in my JDBC code. I am trying to connect through
I have some problem with my code public IQueryable<PageItems> GetPageById(Guid Id) { var xml
I have some problem with my cfml website. I have used the below code
i am new in JSP,i have some problem with the following code : <%@
I have some code that I'd like to run on a page. My problem
I have come across an annoying problem while writing some PHP4 code. I renamed
I have some projects on bzr code repositories shared with colleagues. Problem is, I

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.