旁路按TAB键时只读细胞的DataGridView [英] Bypass read only cells in DataGridView when pressing TAB key

查看:142
本文介绍了旁路按TAB键时只读细胞的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我的按TAB键时,我如何能绕过只读在DataGridView的细胞一些代码?

Can anyone show me some code of how I could bypass read only cells in DatagridView when pressing TAB key?

推荐答案

重写SelectionChanged事件是正确的做法。属性CurrentCell可用于设置当前小区。你想是这样的:

Overriding the SelectionChanged event is the right approach. The property CurrentCell can be used to set the current cell. You want something like this:

private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
    DataGridViewCell currentCell = dataGridView.CurrentCell;
    if (currentCell != null)
    {
        int nextRow = currentCell.RowIndex;
        int nextCol = currentCell.ColumnIndex + 1;
        if (nextCol == dataGridView.ColumnCount)
        {
            nextCol = 0;
            nextRow++;
        }
        if (nextRow == dataGridView.RowCount)
        {
            nextRow = 0;
        }
        DataGridViewCell nextCell = dataGridView.Rows[nextRow].Cells[nextCol];
        if (nextCell != null && nextCell.Visible)
        {
            dataGridView.CurrentCell = nextCell;
        }
    }
}

您需要添加只和循环而下一个信元不可见或只读读取对于当前小区的测试。您还需要进行检查,以确保您如果所有细胞都是只读永远不循环。

You'll need to add a test for the current cell being read only and loop while the next cell is invisible or read only. You'll also need to check to make sure that you don't loop for ever if all cells are read only.

您将不得不应付情况,显示指数的基础指数太不一样了。

You'll have to cope with the case where the display index is different to the base index too.

要按Tab键时,你需要添加的KeyDown处理程序只是让这种行为:

To get this behaviour just when pressing Tab you'll need to add a KeyDown handler:

private void AlbumChecker_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Tab)
    {
        SelectNextEditableCell(DataGridView dataGridView);
    }
}

和把第一个代码在这个新的方法。

and put the first code in this new method.

您可能要检查的DataGridView已集中了。

You might want to check that the DataGridView has focus too.

这篇关于旁路按TAB键时只读细胞的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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