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

Posted on 2008-11-13
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

    You can change the EditMode property

    Me.DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
    LVL 25

    Expert Comment

    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

    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

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    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…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now