?
Solved

enter key on next Column on datagridview in c# visual stedio

Posted on 2014-12-04
7
Medium Priority
?
1,488 Views
Last Modified: 2016-08-04
i want when user press enter on current cell it move to next Column in datagrid view

i use this code
if (e.KeyCode == Keys.Enter)
            {                
                e.SuppressKeyPress=true;
                int iColumn = dataGridView1.CurrentCell.ColumnIndex;
                int iRow = dataGridView1.CurrentCell.RowIndex;
                if (iColumn == dataGridView1.Columns.Count-1)
                    dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
                else
                    dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];

            }

but it not work when i enter data in cell\
and when it in last cell in not go to next row
0
Comment
Question by:ajaymaster1558
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 9

Expert Comment

by:dustock
ID: 40480698
When you press the enter key to stop the Editing, DataGridView CellEndEdit event occurs.  Then the cursor moves to next row, at this point SelectionChanged event occurs.  Try adding this to your program to change the behavior.

Add some global variables
        private Int32 currentRow;
        private Int32 currentCell;
        private bool resetRow = false;

Open in new window


Add the CellEndEdit event and put in the following code

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            resetRow = true;
            currentRow = e.RowIndex;
            currentCell = e.ColumnIndex;            
        }

Open in new window


Add the SelectionChanged event and put in the following code
        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (resetRow)
            {
                resetRow = false;
                if ((Int32)dataGridView1.CurrentCell.ColumnIndex != dataGridView1.Columns.Count -1)
                    dataGridView1.CurrentCell = dataGridView1.Rows[currentRow].Cells[currentCell + 1];
                else
                    dataGridView1.CurrentCell = dataGridView1.Rows[currentRow + 1].Cells[0];
            }
        }

Open in new window

1
 
LVL 34

Accepted Solution

by:
it_saige earned 2000 total points
ID: 40480706
Which event are you binding this code to?  KeyPress, KeyDown?

It looks like KeyDown.  Let me try to replicate your issue.

-saige-
0
 

Author Closing Comment

by:ajaymaster1558
ID: 40482738
thank
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 

Expert Comment

by:sushil kardhone
ID: 41666945
thanks to giving me the wonderful solution...............
thanks a lot
0
 

Expert Comment

by:Muhammad Nouman kibria
ID: 41742192
I have a question about this solution....
this question is working very good but i am facing this problem
when i press the Enter key it will transfer the edit control to below row and then it will be automatically transfer to the selected row to the next cell.
for more detail of my problem. please handle the Datagridview_RowLeave Event. You will be got it my problem.
0
 

Expert Comment

by:Muhammad Nouman kibria
ID: 41742241
how to handle the RowLeave_event using this code?
0
 
LVL 34

Expert Comment

by:it_saige
ID: 41742584
@Muhammad,

Please submit a new question.  You can always use this question as a reference by posting the questions link in your submission.

Thank you,

-saige-
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question