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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:42:41+00:00 2026-05-31T06:42:41+00:00

i have a datagridview with 5 columns,when i press enter it goes to the

  • 0

i have a datagridview with 5 columns,when i press “enter” it goes to the next cell and when it arrives to the end of the rows when i press enter it adds a new rows,but my problem is when i move to the previous rows after i press enter it jumps the rows and does not go to the next cells,any help?

public partial class Form1 : Form
{
    public static int Col;
    public static int Row;

    public Form1()
    {
        InitializeComponent();
    }       

    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.Rows.Add();           
    }   

    private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {           
        Col = dataGridView1.CurrentCellAddress.X;        
        Row = dataGridView1.CurrentCellAddress.Y;     
    }

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {            
        if (e.KeyChar == (int)Keys.Enter)
        {              
            if (Col + 1 < 5)
            {
                dataGridView1.CurrentCell = dataGridView1[Col + 1, Row];
            }
            else
            {                        
                dataGridView1.Rows.Add();
                dataGridView1.CurrentCell = dataGridView1[Col - 4, Row + 1];
            }
        }
    }
}
  • 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-31T06:42:42+00:00Added an answer on May 31, 2026 at 6:42 am

    Forget about CellEnter event and the Form1_KeyPress event also. Just handle the dataGridView1_KeyDown event like this:

        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                int col = dataGridView1.CurrentCell.ColumnIndex;
                int row = dataGridView1.CurrentCell.RowIndex;
    
                if (col < dataGridView1.ColumnCount - 1)
                {
                    col ++;
                }
                else
                {
                    col = 0;
                    row++;
                }
    
                if (row == dataGridView1.RowCount)
                    dataGridView1.Rows.Add();
    
                dataGridView1.CurrentCell = dataGridView1[col, row];
                e.Handled = true;
            }
        }
    

    Please note that I changed the code a bit, and remember to set the Handled event property to true, otherwise it will process the default behavior.

    Cheers!

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

Sidebar

Related Questions

I have datagridview with checkbox column, but I want, that some of the columns
I have a datagridview made up of multiple rows and columns. I want to
I have created a Datagridview with 4 Columns, EJ: ID, Name, Quantity, other But
I have a DataGridView that is populated with 4 columns and multiple rows of
I have tried to restrict Following EventHandler In specific Columns of Datagridview but fail
i have one datagridview.in my datagridview is having four row and three columns.after enter
I have a DataGridView with custom columns. But when I add a DataGridViewComboBoxColumn and
I have a datagridview with 10 columns. Every month the user will update new
I have dataGridView with many columns and rows. Now I'd like to bind content
I have a DataGridView with 2 columns, with 150 datas => 150 rows. What

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.