Как изменить порядок вкладок в DataGridView? - PullRequest
2 голосов
/ 10 апреля 2009

Мой клиент хочет, чтобы при переходе через ячейки DataGridView следующая текущая ячейка отличалась от ячейки по умолчанию. Какой лучший способ сделать это?

Ответы [ 2 ]

2 голосов
/ 10 апреля 2009

Создайте свой собственный DataGridView, переопределите метод ProcessTabKey. По логике, используйте SetCurrentCellAddressCore для установки следующей активной ячейки.

Обратите внимание, что реализация этого метода по умолчанию учитывает множество различных условий, таких как режим выбора, режим редактирования, состояния строк, границы и т. Д.

Редактировать

Кроме того, вы можете обрабатывать события KeyUp / KeyDown. Хотя в этом есть какое-то странное поведение, и я не тратил много времени, это должно сделать:

Установите для свойства StandardTab сетки значение True и добавьте следующий код:

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 голосов
/ 06 апреля 2015

г. Код Руслана у меня не сработал. Получив его идею, я внес некоторые изменения в код.

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;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...