How to control SendKeys.Send{"tab"} property in Datagridview?

Learner picture Learner · Feb 14, 2013 · Viewed 19k times · Source

I am writing the code in datagridview cell enter event as

private void dgvGoodsRecpt_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    if (dgvGoodsRecpt.CurrentRow.Cells[e.ColumnIndex].ReadOnly)
    {
        SendKeys.Send("{tab}");
    }
}

This code will send the tab to next cell ,if the current cell is ReadOnly true.

It is working well but here my problem is that my datagridview last column is readonly true and the next controls are txtAmount1.Text and txtAmount2.Text.

When I keep on pressing Tab key the focus is going to txtAmount2.Text. But the next to datagridview is txtAmount1.Text. The focus is going to the next control of the imidiate control after datagridview. The focus supposed to go to the txtAmount1.Text control. What should I do? Please help me.

Answer

zmechanic picture zmechanic · Oct 28, 2017

There is a list of predefined string constants for non-alphanumeric keys. For example, {TAB} is for a Tab key. Here is MSDN article with all constants: https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

You may also consider using SendKeys.SendWait instead. This method will ensure that target application processes sent key before your application continues. For example:

SendKeys.SendWait("{TAB}");