How do I change the tab order in a DataGridView?

后端 未结 2 826
我寻月下人不归
我寻月下人不归 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;
        }
    
    0 讨论(0)
  • 2021-01-21 16:38

    Create your own DataGridView, override ProcessTabKey method. Do you logic there, use SetCurrentCellAddressCore to set next active cell.

    Note that the default implementation of that method accounts for many different conditions, such as selection mode, editing mode, row states, bounds etc.

    Edit

    Alternatively, you could handle KeyUp/KeyDown events. Although, there is some weird behavior with it and I didn't spend much time, this should do:

    Set StandardTab property of the grid to True, and add following code:

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: Load Data
    
        dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
    
        if (dataGridView1.CurrentCell.ReadOnly)
            dataGridView1.CurrentCell = GetNextCell(dataGridView1.CurrentCell);
    }
    
    private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Tab)
        {
            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;
    }
    
    0 讨论(0)
提交回复
热议问题