How do I change the tab order in a DataGridView?

后端 未结 2 827
我寻月下人不归
我寻月下人不归 2021-01-21 16:13

My client wants that when tabbing through DataGridView cells the next current cell to be other than the default one. What\'s the best way to accomplish this?

2条回答
  •  [愿得一人]
    2021-01-21 16:27

    Mr. Ruslan code did not worked for me. By fetching his idea, i made some change in code.

    private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                if(dataGridView1.CurrentCell.ReadOnly)
                    dataGridView1.CurrentCell = GetNextCell(dataGridView1.CurrentCell);
                e.Handled = true;
            }
        }
    
        private DataGridViewCell GetNextCell(DataGridViewCell currentCell)
        {
            int i = 0;
            DataGridViewCell nextCell = currentCell;
    
            do
            {
                int nextCellIndex = (nextCell.ColumnIndex + 1) % dataGridView1.ColumnCount;
                int nextRowIndex = nextCellIndex == 0 ? (nextCell.RowIndex + 1) % dataGridView1.RowCount : nextCell.RowIndex;
                nextCell = dataGridView1.Rows[nextRowIndex].Cells[nextCellIndex];
                i++;
            } while (i < dataGridView1.RowCount * dataGridView1.ColumnCount && nextCell.ReadOnly);
    
            return nextCell;
        }
    

提交回复
热议问题