DataGridView: Make Enter Key Move to Next Column

You can do this task by tow methods.

Method 1:

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                 e.Handled = true;
                 SendKeys.Send("{TAB}"); 
            }
        }


Method 2:

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)
                    dataGridView1.CurrentCell = dataGridView1[0, iRow + 1];
                else
                    dataGridView1.CurrentCell = dataGridView1[iColumn + 1, iRow];
 
            }
        }

No comments:

Post a Comment