Can I make DataGridView.EndEdit trigger the CellValidating event?

后端 未结 5 611
醉话见心
醉话见心 2021-01-12 02:14

I\'m using a DataGridView in my WinForms application. My main objective is to make the Enter key not move to the next row in the grid. I still want the enter key to validate

5条回答
  •  长发绾君心
    2021-01-12 02:56

    CellValidating doesn't get called until you change the CurrentCell. So the way I kludged around this was to change the CurrentCell, then switch back to the current one.

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                DataGridViewCell currentCell = CurrentCell;
                EndEdit();
                CurrentCell = null;
                CurrentCell = currentCell;
                return true;
            }
            return base.ProcessDialogKey(keyData);
        }
    

提交回复
热议问题