基本,这个答案https://stackoverflow.com/a/2035138/8425204解决了我的问题,但我不知道C#语言。 视觉基础中有什么吗? 我已经在线尝试了转换器,但由于转换不准确,因此无法解决问题
答案 0 :(得分:0)
这是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#代码的原始答案中未对此进行说明。