首页 > 解决方案 > C#在datagridview中按回车键时如何移动更改焦点

问题描述

(对不起,我英语不好。)

嗨,我想在 gridview 中按下回车键时移动到第四个单元格。

如果焦点是第四个单元格,我想移动下一行的第一个单元格。

在gridview的第一个单元格中输入数据> Enter键>移动到当前行的第四个单元格> Enter键>移动到下一行的第一个单元格> ...

我试过这段代码。

private void ProductInfoView_KeyDown(object sender, KeyEventArgs e)
        {
           if (e.KeyData == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                int irow = ProductInfoView.CurrentCell.RowIndex;
                int icolumn = ProductInfoView.CurrentCell.ColumnIndex;

                if (icolumn == ProductInfoView.Columns.Count - 1)
                {
                    ProductInfoView.CurrentCell = ProductInfoView[0, irow + 1];
                }
                else
                {
                    ProductInfoView.CurrentCell = ProductInfoView[icolumn + 3, irow];

                }
            }
        }

但是e.SuppressKeyPress = true; 没有工作。当我按下回车键时,它进入下一行的第一个单元格。

所以,我尝试了另一个代码。

        private void ProductInfoView_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                int irow = ProductInfoView.CurrentCell.RowIndex;
                int icolumn = ProductInfoView.CurrentCell.ColumnIndex;

                if (icolumn == ProductInfoView.Columns.Count - 1)
                {
                    ProductInfoView.CurrentCell = ProductInfoView[0, irow + 1];
                }
                else
                {
                    ProductInfoView.CurrentCell = ProductInfoView[icolumn + 3, irow];
                }
            }

        }

在这段代码中,移动到第四个单元格有效,但输入键也有效。

焦点移到下一行的第四个单元格。

我该如何解决。

标签: c#

解决方案


推荐阅读