1

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

Pic-Datagridview 不显示文本 >~43,700 个字符

我尝试将单元格设置为 wraptext 并自动调整行和列的大小,但这实际上使问题发生在较少的字符数上(使情况变得更糟)。我还发现了一篇非常古老的先前帖子,它与“ .NET Windows forms DataGridView Cell text消失时以编程方式添加”有些相关。但是,这发生在 4,563 个字符处并且没有得到解决。

另外,我正在使用 Visual Basic。

任何帮助,将不胜感激。

4

1 回答 1

1

我可以重现你的问题。

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
于 2018-10-13T21:38:38.947 回答