Can I make DataGridView.EndEdit trigger the CellValidating event?

后端 未结 5 613
醉话见心
醉话见心 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:35

    JJO's code will crash if cell is in edit mode. Below avoids validation exception:

    DataGridViewCell currentCell = AttachedGrid.CurrentCell;
            try
            {             
                AttachedGrid.EndEdit();
                AttachedGrid.CurrentCell = null;
                AttachedGrid.CurrentCell = currentCell;
            }
            catch 
            {
                AttachedGrid.CurrentCell = currentCell;
                AttachedGrid.CurrentCell.Selected = true; 
            }
    

    Source: Kennet Harris's answer here

提交回复
热议问题