始终保持光标指向datagrid视图的特定列

时间:2014-04-09 12:37:54

标签: c# winforms datagridview

我有一个像这样的数据网格视图: enter image description here

我正在开发Windows应用程序..其中驱动程序列我必须给出驱动程序ID ..然后我点击enter..so那时光标正在移动下一行释放按钮.. 我不想移动光标下一行按钮..单击输入后我想将我的光标移到下一行驱动程序ID列.. 我如何只在特定列上设置Tab键顺序。

任何帮助都非常明显..

1 个答案:

答案 0 :(得分:0)

这是一个代码,用于创建一个派生自DataGridView的自定义类,并设置ENTER键的行为,使其沿着行移动但跳过最后一列(如OP情况下的Button列)

class MyCustomDGV : DataGridView
{
    private int currentCol = 0;
    private int currentRow = 0;

    // Store the current cell position.
    protected override void OnCellEnter(DataGridViewCellEventArgs e)
    {
        currentCol = e.ColumnIndex;
        currentRow = e.RowIndex;

        base.OnCellEnter(e);
    }

    protected override bool ProcessDialogKey(Keys keyData)
    {
        // Extract the key code from the key value. 
        Keys key = (keyData & Keys.KeyCode);

        // Handle the ENTER key as if it were a RIGHT ARROW key.  
        if (key == Keys.Enter)
        {
            if (currentCol >= this.ColumnCount - 2)
            {
                return this.ProcessEnterKey(keyData);
            }
            else
            {
                return this.ProcessRightKey(keyData);
            }

        }

        return base.ProcessDialogKey(keyData);
    }

    protected override bool ProcessDataGridViewKey(KeyEventArgs e)
    {
        // Handle the ENTER key as if it were a RIGHT ARROW key.  
        if (e.KeyCode == Keys.Enter)
        {
            if (currentCol >= this.ColumnCount - 2)
            {
                return this.ProcessEnterKey(e.KeyData);
            }
            else
            {
                return this.ProcessRightKey(e.KeyData);
            }
        }
        return base.ProcessDataGridViewKey(e);
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            if (currentCol >= this.ColumnCount - 2)
            {
                e.SuppressKeyPress = true;
                e.Handled = true;
                this.ProcessEnterKey(e.KeyData);
                return;
            }
            else
            {

            }
        }

        base.OnKeyDown(e);
    }
}
相关问题