Link to home
Start Free TrialLog in
Avatar of cobolinx1
cobolinx1

asked on

hitting the enter button in datagridviewtextcell

I want to be able to hit the enter button in a datagridviewtextcell and have it create a new line within the cell and autoformat the size of the cell. How would I do that?
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

Set (or leave) the datagridview property "AllowUserToAddRows" to true for a new line.

To autoresize the cell, call an autoresize routine from the CellEndEdit.

I am not sure you can create a new line in a cell if that is what you want.
Avatar of cobolinx1
cobolinx1

ASKER

Is there a way to capture the keystroke in the cell and if its the enter button make the cell values = cellvalue & vbnewline?
yes you can ....
use Editing Control Showing event like this


Private WithEvents thiscontrol As TextBox
   
    Private Sub MyDataGridView_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles MyDataGridView.EditingControlShowing
        Try
            If MYDataGridView.CurrentCell.ColumnIndex = "YOURCOLUMNINDEX" Then
                thiscontrol = CType(e.Control, TextBox)
  Else
                thiscontrol = Nothing

            End If
Private Sub thiscontrol_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles thiscontrol.KeyDown
    If e.KeyCode = Keys.Return Then

.....WHAT YOU WANT.....
End Sub

Open in new window

Nope it interprets the enter key as moving to the next row before it can fire off the keydowncode
think...... then how do you move FROM the cell if the enter key is "escaped" to a vbnewline?

(Actually, I am surprised that you can do a vbnewline in a datagridviewcell, but then again, something new a day, not bad.)
...I would actually be mouse clicking off it and when it lost focus Id create a new line in the datagridview.. I guess I could have the user type in a textbox and when they click a button they could add it to the datagridview...no other way though??
mmm...so you need a multiline textbox????
ASKER CERTIFIED SOLUTION
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece 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
SOLUTION
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
I would have to capture the keydown before the cellendedit and then then try to set the cursor back. I think I'll just have them hit shift and enter. Thanks.