Вот код в vb.net:
Сначала CustomDataGridViewTextBoxEditingControl
:
Public Class CustomDataGridViewTextBoxEditingControl
Inherits DataGridViewTextBoxEditingControl
Public Overrides Function EditingControlWantsInputKey(keyData As Keys, 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(e As KeyEventArgs)
Select Case e.KeyCode And Keys.KeyCode
Case Keys.Enter
Dim oldSelectStart = Me.SelectionStart
Dim CurrentText = Me.Text
me.Text = String.Format("{0}{1}{2}",CurrentText.Substring(0,me.SelectionStart), vbCrLf,CurrentText.Substring(Me.SelectionStart + Me.SelectionLength))
Me.SelectionStart = oldSelectStart + vbCrLf
Case Else
End Select
MyBase.OnKeyDown(e)
End Sub
End Class
Затем вы создаете CustomDataGridViewTextBoxCell
:
Public Class CustomDataGridViewTextBoxCell
Inherits DataGridViewTextBoxCell
Public Overrides ReadOnly Property EditType As Type
Get
Return GetType(CustomDataGridViewTextBoxEditingControl)
End Get
End Property
End Class
Тогда определите класс DataGridViewCustomColumn
:
Public Class DataGridViewCustomColumn
Inherits DataGridViewColumn
Public Sub New()
Me.CellTemplate = New CustomDataGridViewTextBoxCell()
End Sub
End Class
А потом в своем коде вы пишете:
Dim col = new DataGridViewCustomColumn
DataGridView1.Columns.Add( col)
Вы должны установить свойства DefaultCellStyle.WrapMode
на True
и AutoSizeRowMode
на AllCells
. Это не было указано в исходном ответе в коде C #.