【问题标题】:How can I change what happens when "enter" key is pressed on a DataGridView?如何更改在 DataGridView 上按下“enter”键时发生的情况?
【发布时间】:2010-03-27 07:47:20
【问题描述】:

当我编辑一个单元格并按下回车键时,会自动选择下一行,我想留在当前行...除了 EndEdit,我什么都不想做。

我有这个:

private void dtgProductos_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            dtgProductos[e.ColumnIndex, e.RowIndex].Selected = true; //this line is not working
            var index = dtgProductos.SelectedRows[0].Cells.IndexOf(dtgProductos.SelectedRows[0].Cells[e.ColumnIndex]);
            switch (index)
            {
                case 2:
                    {
                        dtgProductos.SelectedRows[0].Cells[4].Selected = true;
                        dtgProductos.BeginEdit(true);
                    }
                    break;
                case 4:
                    {
                        dtgProductos.SelectedRows[0].Cells[5].Selected = true;
                        dtgProductos.BeginEdit(true);
                    }
                    break;
                case 5:
                    {
                        btnAddProduct.Focus();
                    }
                    break;
                default:
                    break;
            }
        }

所以当我编辑不是最后一行的行时,我会收到此错误:

Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    我认为您需要覆盖 ProcessEnterKey 以不将焦点推进到下一行。

    也许this thread 会有所帮助。

    【讨论】:

    • 我找不到要覆盖的方法,你能帮我解决这个问题吗?
    • 现在我明白了...我将覆盖方法留空,但仍然出现错误 =(
    • 添加了一个链接,我发现谷歌搜索短语“更改输入键 datagridview 的默认行为”(因为推进一行是默认行为)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2013-11-23
    相关资源
    最近更新 更多