我可以重现你的问题。
DataGridViewTextBoxCell.Paint方法使用TextRenderer.DrawText
调用的 PaintPrivate Method 中的方法。
这篇文章描述了TextRender.MeasureText
如果文本超过 43679 个字符,该方法返回无效值的问题,这与您发现的限制相似。由于MeasureText
还涉及对 的调用DrawText
,因此这两个问题是相关联的。
一个简单的解决方法是处理DataGridView.CellFormatting 事件并将格式化字符串截断为魔术字符串长度。
Const magicMaxStringLength As Int32 = 43679
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
Dim str As String = TryCast(e.Value, String)
If str IsNot Nothing AndAlso str.Length >= 43679 Then
e.Value = str.Substring(0, 43679)
e.FormattingApplied = True
End If
End Sub
请注意,此截断不会更改基础单元格值。
Edir:以上允许 DGV 显示文本,但似乎同样的限制也适用于DataGridViewTextBoxEditingControl
. 因此,文本在编辑时不可见。要克服此限制,您可以交换一个RichTextBox
以用作编辑控件。
Private rtbEdit As New RichTextBox
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Dim tb As System.Windows.Forms.DataGridViewTextBoxEditingControl = TryCast(e.Control, System.Windows.Forms.DataGridViewTextBoxEditingControl)
If tb IsNot Nothing AndAlso tb.Text.Length >= magicMaxStringLength Then
rtbEdit.Text = tb.Text
rtbEdit.Margin = New Padding(0)
rtbEdit.AutoSize = False
rtbEdit.Font = tb.Font
rtbEdit.ClientSize = DataGridView1.EditingPanel.ClientSize
rtbEdit.WordWrap = False
rtbEdit.Multiline = tb.Multiline
DataGridView1.EditingPanel.Controls.Remove(e.Control)
DataGridView1.EditingPanel.Controls.Add(rtbEdit)
RemoveHandler DataGridView1.EditingPanel.VisibleChanged, AddressOf DataGridView1EditingPanel_VisibleChanged
AddHandler DataGridView1.EditingPanel.VisibleChanged, AddressOf DataGridView1EditingPanel_VisibleChanged
RemoveHandler DataGridView1.EditingPanel.SizeChanged, AddressOf DataGridView1EditingPanel_SizeChanged
AddHandler DataGridView1.EditingPanel.SizeChanged, AddressOf DataGridView1EditingPanel_SizeChanged
End If
End Sub
Private Sub DataGridView1EditingPanel_SizeChanged(sender As Object, e As EventArgs)
RemoveHandler DataGridView1.EditingPanel.SizeChanged, AddressOf DataGridView1EditingPanel_SizeChanged
rtbEdit.ClientSize = DataGridView1.EditingPanel.ClientSize
End Sub
Private Sub DataGridView1EditingPanel_VisibleChanged(sender As Object, e As EventArgs)
If Not DataGridView1.EditingPanel.Visible Then
DataGridView1.EditingPanel.Controls.Remove(rtbEdit)
DataGridView1.CurrentCell.Value = rtbEdit.Text
RemoveHandler DataGridView1.EditingPanel.VisibleChanged, AddressOf DataGridView1EditingPanel_VisibleChanged
End If
End Sub