I have a winforms application with a DataGridView that has some multi-line text cells. The default behavior of the DataGridViewTextBoxEditingControl seems strange. For some reason, if a cell is being edited and the selection is at the very end of the text (at the end of the very last word on the last line), if the user presses the down arrow key, it sets the selection back to the start of the text in that cell (without leaving edit mode) instead of taking them to the next cell. If down arrow is pressed while editing any line other than the last line, the insertion point moves to the next line as expected. If down arrow is pressed while editing the last line but not at the very end of the line, then editing ends and the next cell (below) becomes the new current cell. Is there any (good/simple) way to change this behavior? I came up with this solution, but I'm not very happy about having to put in kludgey code to fix what appears to be a bug in the .Net control.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (msg.WParam.ToInt32() == (int)Keys.Down &&
IsCurrentCellInEditMode && EditingControl is DataGridViewTextBoxEditingControl &&
((DataGridViewTextBoxEditingControl)EditingControl).SelectionStart == EditingControl.Text.Length)
{
EndEdit();
if (CurrentCellAddress.Y < RowCount - 1)
CurrentCell = this[CurrentCellAddress.X, CurrentCellAddress.Y + 1];
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}