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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:34:05+00:00 2026-06-10T17:34:05+00:00

I’ve got a DataGridView that I populate in this way: the even-numbered rows contain

  • 0

I’ve got a DataGridView that I populate in this way: the even-numbered rows contain “constant” values that are not to be edited by the user. The odd-numbered rows can be edited by the user, but should only contain 0 or 1 characters. If the cell contains a value and the user presses a key, it should first move to the next cell down, and then allow that value to be entered in that next cell. In this way a user can continue pressing a key and the cells below will be populated each time.

I’ve got this code (based on code from David Hall here: How can I programmatically move from one cell in a datagridview to another?):

private void dataGridViewPlatypus_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    int columnIndex = (((DataGridView)(sender)).CurrentCell.ColumnIndex);
    if (columnIndex % 2 == 1) {
        e.Control.KeyPress += TextboxNumeric_KeyPress;
    } 
}

private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
{
    TextBox tb = sender as TextBox; 
    if (tb.TextLength >= 1)
    {
        dataGridViewPlatypus.CurrentCell = dataGridViewPlatypus[
            dataGridViewPlatypus.CurrentCell.ColumnIndex, 
            dataGridViewPlatypus.CurrentCell.RowIndex + 1];
    }
}

This works great the first time I enter a val in a cell that already has a value – it moves to the next cell down and the subsequent key press enters the value there. After that, though, it skips one more cell each time. IOW, if I first enter a “2” in the cell at column 5, row 2, it moves to row 3 (good!); then, though, it moves to row 5, skipping row 4. On the next key press it moves to row 8, skipping rows 6 and 7, and so on.

Why would it act this way, and what is the solution?

UPDATE

Okay, based on LarsTech’s answer below, I’ve now got this code:

private void dataGridViewPlatypus_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
    int columnIndex = (((DataGridView)(sender)).CurrentCell.ColumnIndex);
    if (columnIndex % 2 == 1) {
        e.Control.KeyPress -= TextboxNumeric_KeyPress;
        e.Control.KeyPress += TextboxNumeric_KeyPress;
    }
}

private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e) {
    const int LAST_ROW = 11;
    const int LAST_COL = 15;
    TextBox tb = sender as TextBox;
    if (tb.TextLength >= 1) {
        if (dataGridViewPlatypus.CurrentCell.RowIndex != LAST_ROW) {
            dataGridViewPlatypus.CurrentCell = dataGridViewPlatypus[
                dataGridViewPlatypus.CurrentCell.ColumnIndex,
                dataGridViewPlatypus.CurrentCell.RowIndex + 1];
        } else { // on last row
            if (dataGridViewPlatypus.CurrentCell.ColumnIndex != LAST_COL) {
                dataGridViewPlatypus.CurrentCell =
                    dataGridViewPlatypus[dataGridViewPlatypus.CurrentCell.ColumnIndex + 2, 0];
            } else // on last row AND last editable column
            {
                dataGridViewPlatypus.CurrentCell = dataGridViewPlatypus[1, 0];
            }
        }
    }
}

However, now the problem is that if I am in a cell which has a previous value entered, it won’t overwrite an old value with the new value being entered. So is there a way to not enter another value in this cell while simultaneously allowing a new value to replace an existing value in the cell?

  • 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-10T17:34:07+00:00Added an answer on June 10, 2026 at 5:34 pm

    You are adding more and more keypress events:

    e.Control.KeyPress += TextboxNumeric_KeyPress;
    

    without removing the previous keypress event. So it’s calling it multiple times.

    Try changing it to something like this:

    if (columnIndex % 2 == 1) {
      e.Control.KeyPress -= TextboxNumeric_KeyPress;
      e.Control.KeyPress += TextboxNumeric_KeyPress;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.