0

in a project created in vb.net I have a datagridview in which the user can enter data directly. The columns are Qty, description, price and total. The qty and price columns only admit numbers and comma, the total column is calculated. I would like that when user edit a description cell, if press ENTER create a new line (crlf). Can you help me since I have not found any useful examples on the internet?

  • Set: `DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True`, `DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells` and use `SHIFT+ENTER` to create a new line. – Jimi Feb 08 '19 at 16:51
  • If you don't like the `SHFT+ENTER`, you need to create a custom [DataGridViewTextBoxEditingControl Class](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewtextboxeditingcontrol). – Jimi Feb 08 '19 at 17:11
  • I have just tried what you say in the first post but i don' t know why when i press shift+ enter a new line appear in the cell but the focus pass in another cell... – Salvatore De Cervo Feb 08 '19 at 19:50
  • See whether you have some previously attempted code that gets in the way. Try with a new DataGridView, using just the two liines shown in the first comment. Press `F2` or click a cell twice to enter editing, then press `Shift+Enter` to insert a new line. – Jimi Feb 08 '19 at 20:14
  • Hi Jim i've found the problem. My form have keypreview on and i handle the ENTER keypress to sendkeys TAB for move to next control in form. I don't want to lose this function.I think that the only solution are to implement a custom DataGridViewTextBoxEditingControl Class. I never use that, i need to study it.Tnx! – Salvatore De Cervo Feb 08 '19 at 20:40
  • `Shift+Enter` is not `Enter`. You can simply check whether the `Shift` modifier is included or just pressed (`if ModifierKeys = Keys.Shift (...)`). If it is, don't `TAB` it :) – Jimi Feb 08 '19 at 21:03
  • OMG! if ModifierKeys = Keys.Shift resolve all...now still a problem.I want to increase the height of this cell during editing by a button press but it not work....arg...it's a bad day! – Salvatore De Cervo Feb 08 '19 at 21:16
  • What do you mean with *during editing*? If you click on another Control while in edit mode, this mode ends there. But `DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells` already takes case of this: it modifies the Row height to accomodate the content of the cells, so... To increase/decrease the height of a cell/row **while** editing, you need a custom `DataGridViewTextBoxEditingControl`. – Jimi Feb 08 '19 at 21:23
  • Nice, i think DataGridViewTextBoxEditingControl it's the best solution for me! – Salvatore De Cervo Feb 08 '19 at 21:34
  • In the long run, it will most probably turn out it is. It's a simple class that you can then reuse everywhere, so it's woth it. See here: [DataGridView: How can I make the enter key add a new line instead of changing the current cell?](https://stackoverflow.com/a/2035138/7444103): C# code, but it's the same thing in VB.Net. – Jimi Feb 08 '19 at 21:37
  • I don't know how i can add what i make then i add an answer. I have traslated the code that you have seen to me but it not work...why? – Salvatore De Cervo Feb 09 '19 at 18:18

1 Answers1

0

I've read the post that you say. Converting the code form c# i've obtained

Public Class CustomDataGridViewTextBoxEditingControl
Inherits DataGridViewTextBoxEditingControl

Public Overrides Function EditingControlWantsInputKey(ByVal keyData As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean
    Select Case keyData And Keys.KeyCode
        Case Keys.Enter
            Return True
        Case Else
    End Select
    Return MyBase.EditingControlWantsInputKey(keyData, dataGridViewWantsInputKey)
End Function
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
    Select Case e.KeyCode And Keys.KeyCode
        Case Keys.Enter
            Dim oldSelectionStart As Integer = Me.SelectionStart
            Dim currentText As String = Me.Text
            Me.Text = String.Format("{0}{1}{2}", currentText.Substring(0, Me.SelectionStart), Environment.NewLine, currentText.Substring(Me.SelectionStart + Me.SelectionLength))
            Me.SelectionStart = oldSelectionStart + Environment.NewLine.Length
        Case Else
    End Select

    MyBase.OnKeyDown(e)
End Sub
End Class
Public Class CustomDataGridViewTextBoxCell
Inherits DataGridViewTextBoxCell
Public Overrides ReadOnly Property EditType As Type
    Get
        Return GetType(CustomDataGridViewTextBoxEditingControl)
    End Get
End Property
End Class

Then, in the form that contain datagridview(a test project!) i've added

Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles 
MyBase.Load

    Dim col As DataGridViewColumn = Me.DataGridView2.Columns(2)
    col.CellTemplate = New CustomDataGridViewTextBoxCell

End Sub

but not working. Whats wrong?