Solved

hitting the enter button in datagridviewtextcell

Posted on 2010-08-30
10
479 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
  • 4
  • 4
  • 2
10 Comments
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:cobolinx1
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:cobolinx1
Comment Utility
...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
Comment Utility
mmm...so you need a multiline textbox????
0
 
LVL 18

Accepted Solution

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now