Skip to content

Instantly share code, notes, and snippets.

@aindong
Created December 3, 2014 08:21
Show Gist options
  • Save aindong/bd9a1652dab5134b0137 to your computer and use it in GitHub Desktop.
Save aindong/bd9a1652dab5134b0137 to your computer and use it in GitHub Desktop.
Listen for the enter key and move on the next column or row on datagridview
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
try
{
int icolumn = dgvCart.CurrentCell.ColumnIndex;
int irow = dgvCart.CurrentCell.RowIndex;
if (keyData == Keys.Enter)
{
if (icolumn == dgvCart.Columns.Count - 1)
{
dgvCart.Rows.Add();
dgvCart.CurrentCell = dgvCart[0, irow + 1];
}
else
{
dgvCart.CurrentCell = dgvCart[icolumn + 1, irow];
}
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
catch (InvalidOperationException ex)
{
return false;
}
catch(NullReferenceException ex)
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment