Solved

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

Posted on 2014-12-04
7
1,262 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
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 32

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 32

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Please explain: Aspect Oriented Programming 2 57
Code enhancement 5 13
Get String split 5 33
Showdialog 8 21
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
This video discusses moving either the default database or any database to a new volume.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now