If tab index is on last row and column of datagridview, if I press tab key at this point, it is moving to the first row and column of datagridview instead of next control(button). Can someone suggest me how to stop the tab indexing on last row and move to next control. I tried this code.
private void dgCoreRoutes_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
if (dgCoreRoutes.CurrentCell.RowIndex == dgCoreRoutes.Rows.Count-1)
{
dgCoreRoutes.TabStop = true;
}
if (dgCoreRoutes.CurrentCell.ReadOnly)
{
dgCoreRoutes.CurrentCell = GetCoreRoutesGridNextCell(dgCoreRoutes.CurrentCell);
e.Handled = true;
}
}
private DataGridViewCell GetCoreRoutesGridNextCell(DataGridViewCell currentCell)
{
int i = 0;
DataGridViewCell nextCell = currentCell;
do
{
int nextCellIndex = (nextCell.ColumnIndex + 1) % dgCoreRoutes.ColumnCount;
int nextRowIndex = nextCellIndex == 0 ? (nextCell.RowIndex + 1) % dgCoreRoutes.RowCount : nextCell.RowIndex;
nextCell = dgCoreRoutes.Rows[nextRowIndex].Cells[nextCellIndex];
i++;
}
while (i < dgCoreRoutes.RowCount * dgCoreRoutes.ColumnCount && nextCell.ReadOnly);
return nextCell;
}
I returned false and set focus to next control, now its working for me.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if(keyData == Keys.Tab && dgCoreRoutes.CurrentCell == dgCoreRoutes.Rows[dgCoreRoutes.Rows.Count - 1].Cells[(int)enGridColumns.Margin])
{
btnNext.Focus();
return false;
}
else if (keyData == Keys.Tab && dgCoreRoutes.CurrentCell.ReadOnly)
{
dgCoreRoutes.CurrentCell = GetCoreRoutesGridNextCell(dgCoreRoutes.CurrentCell);
return true;
}
else
return base.ProcessCmdKey(ref msg, keyData);
}