[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2014-12-04
7
Medium Priority
?
1,542 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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 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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 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