Hi @rahul kumar , Welcome to Microsoft Q&A.
SelectNextControl doesn't work very well here, You can use sendtab directly instead. In your scenario, the code below works directly for you.
I found that you need to keep the first line, you can choose to use tabindex directly to modify.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (CurrentCell != null)
{
int currentrowposition = CurrentCell.RowIndex; // it must stay here
int currentcolumnposition = CurrentCell.ColumnIndex; // it must stay here
if (keyData == Keys.Enter)
{
if (currentcolumnposition == 0 && string.IsNullOrEmpty(this.CurrentCell.Value?.ToString()))
{
Control nextControl = GetNextControlWithTabIndex(this.Parent, TabIndex + 1);
if (nextControl != null)
{
nextControl.Select();
}
return true;
}
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
private Control GetNextControlWithTabIndex(Control container, int tabIndex)
{
foreach (Control control in container.Controls)
{
if (control.TabStop && control.TabIndex == tabIndex)
{
return control;
}
}
return null;
}
SelectNextControl doesn't work very well here, You can use sendtab directly instead. In your scenario, the code below works directly for you.
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
this.AllowUserToAddRows = true;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (CurrentCell != null)
{
int currentrowposition = CurrentCell.RowIndex; // it must stay here
int currentcolumnposition = CurrentCell.ColumnIndex; // it must stay here
if (keyData == Keys.Enter)
{
if (currentcolumnposition == 0 && string.IsNullOrEmpty(this.CurrentCell.Value?.ToString()))
{
this.AllowUserToAddRows = false;
SendKeys.Send("{Tab}");
return true;
}
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.