How to include the 'Return' in the editing of a Datagridview cell?

I have a datagridview box that allows a user to edit the contents of the cell.   It is a memo field that a user edits and may need to use 'enter' (ie. Return) to go to the next line.  

However, when you hit enter, it jumps out to the next cell.

Any way to code it so that the Return acutally stays within the current cell and goes to the next line in the cell?

Who is Participating?
Hello, rrowe68,
If it's appropriate, you can also do this at design time.  Click on the "..." beside Collection in the Properties list and choose the appropriate column.  Click the "..." beside the DefaultCellStyle property to open the "CellStyle Builder".  Then choose "True" for WrapMode".

Your users will then be able to enter a line break into the cell.  But note that to do this they must hold the <Shift> key down while striking the <Enter> key.   (It may be possible to capture the <Enter> key and translate it to <Shift><Enter> in a keyboard event, but I haven't tried this.)

Jorge PaulinoIT Pro/DeveloperCommented:
You can change the EditMode property

Me.DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
You have to capture the key strokes "on change event" of the celland if it is enter key then focus to next line.

Below URL has code snippet.
rrowe68Author Commented:
I added this and it worked.   I captured the Enter Key and replaced it with Shift Enter.

 Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

        If keyData = Keys.Enter Then
            Dim ctl As Control = Me.ActiveControl
            If Not (ctl Is Nothing) Then
                If (TypeOf ctl Is DataGridViewTextBoxEditingControl) Then
                    Return True
                End If
            End If
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.