Link to home
Start Free TrialLog in
Avatar of rrowe68
rrowe68

asked on

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?

Thanks.
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

You can change the EditMode property
 
Ex.

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.

http://it.gps678.com/5/f0f4c141ed09ec21.html
ASKER CERTIFIED SOLUTION
Avatar of omegaomega
omegaomega
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rrowe68
rrowe68

ASKER

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
                    SendKeys.Send("+~")
                    Return True
                End If
            End If
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function