I can reproduce your issue.
The DataGridViewTextBoxCell.Paint method uses the TextRenderer.DrawText
method in the called PaintPrivate Method.
This post describes an issue with the TextRender.MeasureText
method returning invalid values if the text exceeds 43679 characters which is similar to the limit you discovered. Since the MeasureText
also involves a call to DrawText
, these two issues are linked.
A simple workaround would be to handle the DataGridView.CellFormatting Event and truncate the formatted string to magic string length.
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
Note that this truncation does not alter the underlying cell value.
Edir: The above allows the DGV to display the text, however it seems the same limitation also applies to the DataGridViewTextBoxEditingControl
. Thus the text is not visible while editing. To overcome this limitation, you can swap a RichTextBox
for use as the editing control.
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