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?
0
votes
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?
DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
,DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
and useSHIFT+ENTER
to create a new line. – JimiSHFT+ENTER
, you need to create a custom DataGridViewTextBoxEditingControl Class. – JimiF2
or click a cell twice to enter editing, then pressShift+Enter
to insert a new line. – Jimi