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:
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)
{
dataGridView1
.Rows[dataGridView1.CurrentCell.RowIndex]
.Cells[dataGridView1.CurrentCell.ColumnIndex + 1]
.Selected = true;
e.Handled = true;
return;
}
dataGridView1
.Rows[dataGridView1.CurrentCell.RowIndex]
.Cells[0]
.Selected = true;
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.