代码之家  ›  专栏  ›  技术社区  ›  Luiscencio

如何更改在DataGridView上按“回车”键时发生的情况?

  •  1
  • Luiscencio  · 技术社区  · 14 年前

    当我编辑一个单元格并按回车键时,下一行将被自动选中,我希望保留当前行…我什么都不想发生,除了爱。

    我有这个:

    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.
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   John    14 年前

    我认为您需要重写processenterkey以避免将焦点提前到下一行。

    也许吧 this thread 会有帮助的。