Solved

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

Posted on 2014-12-04
7
1,434 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 500 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
Independent Software Vendors: 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!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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