• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

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?
0
cobolinx1
Asked:
cobolinx1
  • 4
  • 4
  • 2
2 Solutions
 
nepaluzCommented:
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.
0
 
cobolinx1Author Commented:
Is there a way to capture the keystroke in the cell and if its the enter button make the cell values = cellvalue & vbnewline?
0
 
John (Yiannis) ToutountzoglouCommented:
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cobolinx1Author Commented:
Nope it interprets the enter key as moving to the next row before it can fire off the keydowncode
0
 
nepaluzCommented:
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.)
0
 
cobolinx1Author Commented:
...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??
0
 
John (Yiannis) ToutountzoglouCommented:
mmm...so you need a multiline textbox????
0
 
John (Yiannis) ToutountzoglouCommented:
so in my previous code set  thiscontrol.Multiline = True
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)
                thiscontrol.Multiline = True
  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
     thiscontrol.text=thiscontrol.text & VbNewLine
.....WHAT YOU WANT.....
End Sub

Open in new window

0
 
John (Yiannis) ToutountzoglouCommented:
this is one option ...
the other way is to set Default Cell style Wrap mode to true ...and to add a new line while editing press Shift_Enter (that is by deafault)..
0
 
cobolinx1Author Commented:
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now