We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

multiline input in datagridview cell

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:

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
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!
Edit/Delete Message


2 Replies

JS Jeba S Syncfusion Team December 14, 2007 06:25 AM UTC

Hi cecilia,

Thank you for posting query to us.

Our Syncfusion GridDataBoundGrid has direct support for multiline cell. For multiline cell , you can handle Model.QueryCellInfo for GridDataBoundGrid and set the style properties AllowEnter, AutoSize and WrapText to true(this is true by default).The property AllowEnter should let you type an enter key into the cell. Autosize should allow the cell to grow as you type.

Below is the code snippets:

private void Model_QueryCellInfo(object sender, Syncfusion.Windows.Forms.Grid.GridQueryCellInfoEventArgs e)
{
if(e.ColIndex==3 && e.RowIndex>0 )
{
e.Style.BackColor=Color.LightPink;
e.Style.AllowEnter=true;
e.Style.WrapText=true;
e.Style.AutoSize=true;
}
}


Please refer the sample which illustrates the above said feature:
http://websamples.syncfusion.com/samples/Grid.Windows/GDBGMultiline/main.htm


Kindly let us know if you need any further assistance.

Best Regards,
Jeba.



JS Jeba S Syncfusion Team December 14, 2007 06:29 AM UTC

Hi cecilia,

Sorry for the broken link. Please refer the sample in this link:
http://websamples.syncfusion.com/samples/Grid.Windows/GDBGMultiline/main.htm


Kindly let us know if this helps.

Best Regards,
Jeba.


Loader.
Live Chat Icon For mobile
Up arrow icon