Reputation: 122
I want to set tab order only on a particular column. such as i have 2 columns (ID and Name). so tab reflects only on "Name" column . When i press tab it goes to next line in the same column vertically.
Upvotes: 2
Views: 2668
Reputation: 4473
Columns in dataridview have no tab order
1) You can either override the keydown event for handling tab.
2) Another easy way is by pressing Up, Down, Left, Right keys to navidate through the datagrid view.
Upvotes: 0
Reputation: 63347
I think you have to override the ProcessDataGridViewKey
method to catch the Tab
key and select the cell yourself, like this:
public class CustomDataGridView : DataGridView
{
//This contains all the column indices in which the tab will be switched vertically. For your case, the initial index is 1 (the second column): VerticalTabColumnIndices.Add(1);
public List<int> VerticalTabColumnIndices = new List<int>();
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab&&VerticalTabColumnIndices.Contains(CurrentCell.ColumnIndex))
{
int i = (CurrentCell.RowIndex + 1) % Rows.Count;
CurrentCell = Rows[i].Cells[CurrentCell.ColumnIndex];
return true;//Suppress the default behaviour which switches to the next cell.
}
return base.ProcessDataGridViewKey(e);
}
}
//or simply you can handle the Tab key in a KeyDown event handler
private void KeyDownHandler(object sender, KeyEventArgs e){
if(e.KeyCode == Keys.Tab){
e.Handled = true;
//remaining code...
//.....
}
}
//in case you are not familiar with event subscribing
yourDataGridView.KeyDown += KeyDownHandler;
Upvotes: 4