Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi , i have created a custom datagridview and trying to move my cell to the right when i presses enter key (instead of going to down on pressing enter key ) but i found that i have to press enter twice to move rightwards . when i presses enter key first time , it still go downwards (default way) and then if i presses enter again ,then it moves to right .


What I have tried:

protected override bool ProcessDataGridViewKey(KeyEventArgs e)
     {
         if (e.KeyCode == Keys.Enter)
         {
             // Move the focus to the next column and row when Enter key is pressed
             if (CurrentCell.ColumnIndex == ColumnCount - 1)
             {
                 if (CurrentCell.RowIndex == RowCount - 1)
                 {
                     // Reached the last cell, move focus to the first cell
                     CurrentCell = Rows[0].Cells[0];
                 }
                 else
                 {
                     // Move to the first cell of the next row
                     CurrentCell = Rows[CurrentCell.RowIndex + 1].Cells[0];
                 }
             }
             else
             {
                 // Move to the next column
                 CurrentCell = Rows[CurrentCell.RowIndex].Cells[CurrentCell.ColumnIndex + 1];
             }

             BeginEdit(true);
             e.Handled = true;
             return true;
         }

         return base.ProcessDataGridViewKey(e);
     }
Posted
Updated 1-Jul-23 22:48pm
v4
Comments
[no name] 1-Jul-23 10:04am    
I don't see any "e.Handled = true" anywhere ... which means you're "adding behaviour" and never "replacing" it.
Ankit Goel 1-Jul-23 10:13am    
i added e.Handled = true but still no luck
[no name] 1-Jul-23 20:50pm    
It makes a difference where you add it.
Ankit Goel 2-Jul-23 2:05am    
Sir , had you even tried my code ??? i had updated my question

Microsoft Excel moves the cursor down a row if the enter key is used. I would recommend using this as it is familiar.

However, to move the cursor right, not down, on using the Enter key, it is possible. The following code will wrap the cursor right, then down, then reset to the first cell in the first row:
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        InitData();
        dataGridView1.KeyDown += dataGridView1_KeyDown;
    }

    public List<Person> People { get; set; } = new List<Person>
    {
        new Person { Age = 21, FirstName = "Paul", LastName = "McCartney" },
        new Person { Age = 22, FirstName = "John", LastName = "Lennon" },
        new Person { Age = 23, FirstName = "George", LastName = "Harrison" },
        new Person { Age = 24, FirstName = "Ringo", LastName = "Starr" },
    };

    private void InitData()
    {
        dataGridView1.MultiSelect = false;
        dataGridView1.DataSource = People;
        dataGridView1.Rows[0].Cells[0].Selected = true;
    }

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            if (dataGridView1.CurrentCell.ColumnIndex
                < dataGridView1.ColumnCount - 1)
            {
                // move to the next cell on the right
                dataGridView1
                    .Rows[dataGridView1.CurrentCell.RowIndex]
                    .Cells[dataGridView1.CurrentCell.ColumnIndex + 1]
                    .Selected = true;
                e.Handled = true;
                return;
            }

            // move to the first cell of the current row
            dataGridView1
                .Rows[dataGridView1.CurrentCell.RowIndex]
                .Cells[0]
                .Selected = true;

            // move down one row only if there is another, 
            //  else, move to the first row
            if (dataGridView1.CurrentCell.RowIndex
                < dataGridView1.RowCount - 1)
                dataGridView1
                    .Rows[dataGridView1.CurrentCell.RowIndex + 1]
                    .Cells[dataGridView1.CurrentCell.ColumnIndex]
                    .Selected = true;
            else
                dataGridView1
                    .Rows[0]
                    .Cells[dataGridView1.CurrentCell.ColumnIndex]
                    .Selected = true;
            e.Handled = true;
        }
    }
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }

}

Create a new WinForm app, add a DataGridView control, then copy paste the above code. You can run and see how it works.
 
Share this answer
 
Comments
Ankit Goel 2-Jul-23 4:52am    
Although its working but i would like to found out why my code is not working
Graeme_Grant 2-Jul-23 4:56am    
Notice how I set the current cell? I don't do it directly, I set the row and cell based on the current cell set. Once I set the cell, I set Handled = True; Fire up a new project and set breakpoints to see how it works.
The question you should be asking is "why am I trying to subvert a standard mechanism that users are familiar with?"

You are having problems because the actions associated with ENTER in Windows apps is handled by and large by the system: it is trying to do what the user expects and move to the next line and that gets in the way of what you are trying to do.
The user expects ENTER to go to the next line: TAB goes to the right!

If you subvert this, you make problems for yourself because you need to both stop the system doing it's thing, and teach the user to use your app differently from every other app he is familiar with using. And the latter is a challenge: users don't like change, don't read manuals - then just dive in and then complain when what they expect to happen doesn't.

If you want users to hate your app (and probably you as well) then subvert standard operations. But I'd strongly recommend you don't!
 
Share this answer
 
Comments
Ankit Goel 2-Jul-23 3:22am    
Ok that's cool . But that's not the case with every app . Every app has its own features . can you imagine the case if the user wants to use datagridview as a data entry form and on pressing just enter key he can fill a row , then moves to another row on pressing enter . it would be damn easy for him . he has to use arrow keys if he goes with traditional approach . please stop making people suggest and make them believe that what you think is not good and hate would be hated by others too . Its just too easy to comment and not provide any solution .
OriginalGriff 2-Jul-23 3:52am    
That's what TAB is there for: to enter the data and move right along the row while ENTER moves down through the column.
Standards are there for a reason and while all apps have their own features you undermine standards at your peril.
Ankit Goel 2-Jul-23 4:03am    
That quite wonderful , when some people thinks that a standard make by someone fits to all . its like one size fits to all . If this is the case for a standard to exist , then all the apps having datagridview will start behaving the same and there would be no difference between the poor and badly design . anyway thanks for suggestion. i will try to include "why am I trying to subvert a standard mechanism that users are familiar with?" in my every question .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900