在Datagridview Vb.net中消失了超过43,700个字符的文本

时间:2018-10-13 12:24:09

标签: vb.net datagridview longtext

我有一个显示数据库条目的DataGridView。问题是我的很多很长的琴弦都没有出现在单元格中。数据在那里,是因为我可以选择单元格,复制内容,然后将其粘贴到文本文件中,然后它们就在那里。一旦超过〜43,700个字符,Datagridview就好像将文本颜色更改为白色。有关如何解决此问题的任何想法?

Pic-Datagridview not displaying text >~43,700 characters

我尝试将单元格设置为自动换行并自动调整行和列的大小,但这实际上使问题出现在字符数较少的情况下(使情况更糟)。   我还发现了很旧的以前的帖子,在“ .NET Windows forms DataGridView Cell text disappears when added programatically”上有些相关。但是,这种情况发生在4,563个字符处,尚未解决。

此外,我正在使用Visual Basic。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我可以重现您的问题。

DataGridViewTextBoxCell.Paint方法在被调用的PaintPrivate Method中使用TextRenderer.DrawText方法。

This post描述了一种TextRender.MeasureText方法的问题,如果文本超过43679个字符(类似于您发现的限制),则返回无效值。由于MeasureText还涉及到对DrawText的调用,因此这两个问题是关联的。

一个简单的解决方法是处理DataGridView.CellFormatting Event,然后将格式化的字符串截断为 magic 字符串长度。

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