This is the real challenge I brings here for you. Solve it if you can.
The Enter Key On Datagridview Is it good Idea? Or Is it possible for any body here to work it as per following condition?.
If not than Datagridview Control is useless on enter key press.
I have Following Custom DGV:
public class MyDataGridView : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Enter)
{
Tab(keyData);
return true;
}
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Tab(e.KeyData);
return true;
}
return base.ProcessDataGridViewKey(e);
}
private void Tab(Keys KeyData)
{
Point cca = CurrentCellAddress;
bool Forward = ((KeyData & Keys.Shift) != Keys.Shift);
if (Forward)
if (cca.Y == Rows.Count - 1) // last row?
if (cca.X == Columns.Count - 1) // last column?
ToNextControl(Forward);
else
ProcessTabKey(KeyData);
else
ProcessTabKey(KeyData);
else
if (cca.Y == 0) // first row?
if (cca.X == 0) // first column?
ToNextControl(Forward);
else
ProcessTabKey(KeyData);
else
ProcessTabKey(KeyData);
}
/// <summary>
/// Go to the next control, forward or backword. This does not support
/// wrapping from the first to the last or the last to the first.
/// </summary>
/// <param name="Forward">Whether to go forward</param>
private void ToNextControl(bool Forward)
{
Control c = Parent.GetNextControl(this, Forward);
while (c != null && !c.TabStop) // get next control that is a tabstop
c = Parent.GetNextControl(c, Forward);
if (c != null)
c.Select();
}
}
The Same work well on normal it's allow focus on next control at enter keypress but it's not work if you set AllowUserToAddRows=false on databound mode. The exact problem creates when you edit last column's cell value and press enter it's not allow to set focus on next control.
How to overcome from this?. how to solve the issue?