views:

721

answers:

4

I have a DataGridView where I manage manually the focused cells when navigating with TAB key. For example, when the first cell from the last row is the last cell than can be navigated in the DataGridView I want that when pressing 'TAB' the focus to go on the next focusable control (a button).

SendKeys.Send("{TAB}") will not work - the focus will go to the second cell on the last row

A: 

Have you tried the SelectNextControl method?

Peter van der Heijden
A: 
Control nextControl = this.dataGridView1.Parent.GetNextControl(this.dataGridView1, true);
Control prevControl = this.dataGridView1.Parent.GetNextControl(this.dataGridView1, false);
TcKs
+1  A: 

If you set the StandardTab property to True then the behavior of the Tab key will change from moving to the next grid cell to moving to the next control on the form. This may be what you want.

If you want to control which grid cell/column/row gets focused then you can handle the ProcessDialogKey and ProcessDataGridViewKey events in your code.

Rune Grimstad
A: 

Like Rune Grimstad said, if you want any tab behavior other than the one provided by default or StandardTab, you will have to implement your own tab behavior.

Create a new Class that inherits from DataGridView.

protected override bool ProcessDialogKey(Keys keyData)
{
    case Keys.Tab | Keys.Shift:
        // implement your logic here.
        break;
    case Keys.Tab:
        // implement your logic here.
        break;
}
Hao Wooi Lim