如何更改DataGridView中的选项卡顺序? [英] How do I change the tab order in a DataGridView?

查看:60
本文介绍了如何更改DataGridView中的选项卡顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户希望在通过DataGridView单元格切换时,下一个当前单元格不是默认单元格。最好的方法是什么?

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?

推荐答案

创建自己的DataGridView,重写ProcessTabKey方法。您在这里逻辑吗,请使用SetCurrentCellAddressCore设置下一个活动单元格。

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.

编辑

或者,您可以处理KeyUp / KeyDown事件。尽管有一些奇怪的行为,而且我并没有花很多时间,但是应该这样做:

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:

将网格的StandardTab属性设置为True,并添加以下代码:

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;
}

这篇关于如何更改DataGridView中的选项卡顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆