0
votes

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?

1
Set: DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True, DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells and use SHIFT+ENTER to create a new line.Jimi
If you don't like the SHFT+ENTER, you need to create a custom DataGridViewTextBoxEditingControl Class.Jimi
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
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
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

1 Answers

0
votes

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?