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

Posted on 2008-11-13
Medium Priority
Last Modified: 2012-05-05
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?

Question by:rrowe68
LVL 48

Expert Comment

ID: 22952712
You can change the EditMode property

Me.DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
LVL 25

Expert Comment

ID: 22952842
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.

LVL 12

Accepted Solution

omegaomega earned 2000 total points
ID: 22952855
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.)


Author Comment

ID: 22952902
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

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question