Hi tex,
Check this example. Now please take its reference and correct your code.
Code
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
dataGridView1.DataSource = dt;
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
int iColumn = dataGridView1.CurrentCell.ColumnIndex;
int iRow = dataGridView1.CurrentCell.RowIndex;
if (iColumn == dataGridView1.Columns.Count - 1 && iRow != dataGridView1.Rows.Count - 1)
{
dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
}
else if (iColumn == dataGridView1.Columns.Count - 1 && iRow == dataGridView1.Rows.Count - 1)
{
}
else
{
dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];
}
}
}
private DataGridViewCell dgvEndEditCell;
private bool _EnterMoveNext = true;
[System.ComponentModel.DefaultValue(true)]
public bool OnEnterKeyMoveNext
{
get
{
return this._EnterMoveNext;
}
set
{
this._EnterMoveNext = value;
}
}
private void dataGridView1_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
this.dgvEndEditCell = dataGridView1[e.ColumnIndex, e.RowIndex];
if (dataGridView1.Rows.Count - 1 == e.RowIndex && e.ColumnIndex != dataGridView1.Columns.Count - 1)
{
dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex + 1, e.RowIndex];
}
}
private void dataGridView1_SelectionChanged(object sender, System.EventArgs e)
{
if (this._EnterMoveNext && MouseButtons == 0)
{
if (this.dgvEndEditCell != null && dataGridView1.CurrentCell != null)
{
if (dataGridView1.CurrentCell.RowIndex == this.dgvEndEditCell.RowIndex + 1
&& dataGridView1.CurrentCell.ColumnIndex == this.dgvEndEditCell.ColumnIndex)
{
int iColNew;
int iRowNew;
if (this.dgvEndEditCell.ColumnIndex >= dataGridView1.ColumnCount - 1)
{
iColNew = 0;
iRowNew = dataGridView1.CurrentCell.RowIndex;
}
else
{
iColNew = this.dgvEndEditCell.ColumnIndex + 1;
iRowNew = this.dgvEndEditCell.RowIndex;
}
dataGridView1.CurrentCell = dataGridView1[iColNew, iRowNew];
}
}
this.dgvEndEditCell = null;
}
}
}
VB.Net
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id", GetType(Integer)),
New DataColumn("Name", GetType(String)),
New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
dataGridView1.DataSource = dt
End Sub
Private Sub dataGridView1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles dataGridView1.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
Dim iColumn As Integer = dataGridView1.CurrentCell.ColumnIndex
Dim iRow As Integer = dataGridView1.CurrentCell.RowIndex
If iColumn = dataGridView1.Columns.Count - 1 AndAlso iRow <> dataGridView1.Rows.Count - 1 Then
dataGridView1.CurrentCell = dataGridView1(0, iRow + 1)
ElseIf iColumn = dataGridView1.Columns.Count - 1 AndAlso iRow = dataGridView1.Rows.Count - 1 Then
Else
dataGridView1.CurrentCell = dataGridView1(iColumn + 1, iRow)
End If
End If
End Sub
Dim dgvEndEditCell As DataGridViewCell
Private _EnterMoveNext As Boolean = True
<System.ComponentModel.DefaultValue(True)> _
Public Property OnEnterKeyMoveNext() As Boolean
Get
Return Me._EnterMoveNext
End Get
Set(ByVal value As Boolean)
Me._EnterMoveNext = value
End Set
End Property
Private Sub dataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dataGridView1.CellEndEdit
Me.dgvEndEditCell = dataGridView1(e.ColumnIndex, e.RowIndex)
If dataGridView1.Rows.Count - 1 = e.RowIndex AndAlso e.ColumnIndex <> dataGridView1.Columns.Count - 1 Then
dataGridView1.CurrentCell = dataGridView1(e.ColumnIndex + 1, e.RowIndex)
End If
End Sub
Private Sub dataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dataGridView1.SelectionChanged
If Me._EnterMoveNext AndAlso MouseButtons = 0 Then
If Me.dgvEndEditCell IsNot Nothing AndAlso dataGridView1.CurrentCell IsNot Nothing Then
If dataGridView1.CurrentCell.RowIndex = Me.dgvEndEditCell.RowIndex + 1 _
AndAlso dataGridView1.CurrentCell.ColumnIndex = Me.dgvEndEditCell.ColumnIndex Then
Dim iColNew As Integer
Dim iRowNew As Integer
If Me.dgvEndEditCell.ColumnIndex >= dataGridView1.ColumnCount - 1 Then
iColNew = 0
iRowNew = dataGridView1.CurrentCell.RowIndex
Else
iColNew = Me.dgvEndEditCell.ColumnIndex + 1
iRowNew = Me.dgvEndEditCell.RowIndex
End If
dataGridView1.CurrentCell = dataGridView1(iColNew, iRowNew)
End If
End If
Me.dgvEndEditCell = Nothing
End If
End Sub
End Class
Screenshot