How can I make the Enter key behave in a Winforms DataGridViewTextBoxCell like it does in a normal Winforms TextBox (add a new line to the text, instead of changing the current cell)?
How can I make the Enter key behave in a Winforms DataGridViewTextBoxCell like it
Share
Well, I found out how to solve the problem. First, create a class called
CustomDataGridViewTextBoxEditingControlwhich derives fromDataGridViewTextBoxEditingControl, and overrideEditingControlWantsInputKeylike this:This stops the
DataGridViewfrom handing the Enter key and changing the current cell. It does not, however, make the Enter key add a new line. (It appears that theDataGridViewTextBoxEditingControlhas removed the functionality of the Enter key). Therefore, we need to overrideOnKeyDownand implement the functionality ourselves, like this:Then, create a class called
CustomDataGridViewTextBoxCellwhich derives fromDataGridViewTextBoxCell, and override theEditTypeproperty to return the type ofCustomDataGridViewTextBoxEditingControl.After you do this, you can set the
CellTemplateproperty on an existing column to aCustomDataGridViewTextBoxCell, or you can create a class derived fromDataGridViewColumnwithCellTemplatepreset to aCustomDataGridViewTextBoxCell, and you will be all set!