multi-line input in datagridview cell

Cecilia

Member
Joined
Aug 14, 2007
Messages
24
Programming Experience
Beginner
Hi, in datagridview, type in "this is the first line", if you press "enter" and type "this is the second line",it finishes editing the current cell and go to the cell right below it, and put "this is the second line" there. What I want is, when I press "enter" and type in , see the following in the same cell:

this is the first line
this is the second line

Here is my vb code:

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

            If keyData = Keys.Enter Or keyData = Keys.Return Then
                    keyData = (Keys.Shift And Keys.Enter)
                    DataGridView1.CurrentCell.Value += Environment.NewLine
                    SendKeys.Send("{End}")
                    
                     With msg
                            .WParam = (Keys.Shift And Keys.Enter)
                     End With
            End If

            Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Actually I didn't write
VB.NET:
                    DataGridView1.CurrentCell.Value += Environment.NewLine
                    SendKeys.Send("{End}")
at the beginning because I thought keyData = (Keys.Shift And Keys.Enter) would change "enter" key to "shift"+"enter", which would do the job I want in datagridview, but it didn't. What it did is basically ignore the "enter" key and append what you type to the end of the text string in the cell. So I add the above two line, hoping that it will add a new line in the same cell and move the cursor to the end, which is the beginning of the new line in the same cell, but what it did is, it did add a new line in the same cell, the cell grows longer, but the cursor sticks to the end of the first line no matter how many new lines you add, only the cell keeps getting longer, but once you click on another cell, which ends the editing of this cell, the cell shrinks back. The following is what I see:

this is the first linethis is
the second line

The cell shows 2 lines only because I set the wrapmode to be true. Where do all the new lines go? I can see them in edit mode. And how can I move the cursor to the right place, that is the beginning of the new line?

Any help is appreciated!
 
Last edited by a moderator:
Just press default keys Shift-Enter to insert linefeed when you're in edit cell.
 
Back
Top