Hello everyone,
We have a condition in which we have a DataGridView and our requirement is that we want to use enter key to move to next cell of the same row and after finishing the row, the focus should go to next row and again perform the same operation.
so for this work we have to use the following code in dataGridView1_KeyDown() event which we get from the properties of the dataGridView1.
The code is like
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
//check for enter key
if (e.KeyCode == Keys.Enter)
{
DataGridView vw = (DataGridView)sender;
//check for last row
if (vw.CurrentCell.RowIndex == vw.Rows.Count - 1)
{
//if it's not the last cell, move to the next one
if (vw.CurrentCell.ColumnIndex != vw.ColumnCount - 1)
{
vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex].Cells[vw.CurrentCell.ColumnIndex + 1];
}
else // if last cell then either enter new raw or move to first cell.
{
//move to first column in new row
vw.Rows.Add(1);
vw.Refresh();
SendKeys.Send("{HOME}");
}
}
else
{
//if it's the last cell, move to the next row, first cell
if (vw.CurrentCell.ColumnIndex == vw.ColumnCount - 1)
{
vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex + 1].Cells[0];
}
else
{
//move to the next cell on the current row
vw.CurrentCell = vw.Rows[vw.CurrentCell.RowIndex].Cells[vw.CurrentCell.ColumnIndex + 1];
}
}
//mark the keycode as handled
e.Handled = true;
}
}
If you have any query regarding this you can ask me anytime
Also all kind of comments are always welcome.
Thanks
@vg
No comments:
Post a Comment