Solved

hitting the enter button in datagridviewtextcell

Posted on 2010-08-30
10
483 Views
Last Modified: 2012-05-10
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
Comment
Question by:cobolinx1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 33560905
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
 

Author Comment

by:cobolinx1
ID: 33561197
Is there a way to capture the keystroke in the cell and if its the enter button make the cell values = cellvalue & vbnewline?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33561410
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!

 

Author Comment

by:cobolinx1
ID: 33562300
Nope it interprets the enter key as moving to the next row before it can fire off the keydowncode
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33562305
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
 

Author Comment

by:cobolinx1
ID: 33562416
...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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33562956
mmm...so you need a multiline textbox????
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 33563051
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
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 500 total points
ID: 33563126
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
 

Author Closing Comment

by:cobolinx1
ID: 33568054
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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