Solved

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

Posted on 2014-12-04
7
1,322 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 33

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 33

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

10 Experts available now in Live!

Get 1:1 Help Now