【问题标题】:Change the three dots (ellipses) (...) to custom characters in VB.Net DataGridView when cell content is truncated当单元格内容被截断时,将三个点(省略号)(...)更改为 VB.Net DataGridView 中的自定义字符
【发布时间】:2023-11-21 15:34:01
【问题描述】:

我正在 VB.Net 中开发一个项目,我正在使用古吉拉特语字体(非 Unicode)。

我已经放置了一个 DaraGridView (DGV) 并在 DGV 中显示存储在数据库中的数据。

在 DGV 中,如果单元格的内容被截断,DGV 显示椭圆(三个点)(...);但由于字体设置为古吉拉特语字体,它会显示古吉拉特语字母“ઈઈઈ”而不是“...”,因为古吉拉特语字符“ઈ”是用英文“.”编码的。 (点)字符。

在古吉拉特语字体中,“。”可以用英文字符“P”生成。

那么,如何将省略号更改为英文“P”字符?或者我怎样才能完全删除省略号?

我在 2010 年 8 月 31 日星期二下午 1:33 找到了 berc 的类似解决方案: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/95c8963f-9832-4e2d-b057-3590afe3b65d

但我不确定它是否是完美的和/或唯一的方法,它是否真的有效。 如果上述 MSDN 链接上的解决方案很好,我可以从中删除多少代码和/或我需要更多代码才能使其完全正常工作?

【问题讨论】:

    标签: vb.net datagridview cell truncate


    【解决方案1】:

    您可以覆盖CellFormatting 事件:

    Private Sub DGV_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DGV.CellFormatting
        'Check for null values
        If (e Is Nothing) OrElse (e.Value Is Nothing) Then Return
        'Grab the current cell
        Dim C = DirectCast(sender, DataGridView).Rows(e.RowIndex).Cells(e.ColumnIndex)
        'I'm not sure if its my testbed or what but occasionally I was getting NULL here so check for that
        If (C Is Nothing) Then Return
    
        'Measure the string, if it fits, use it as is
        If TextRenderer.MeasureText(e.Value.ToString(), C.InheritedStyle.Font).Width <= C.Size.Width Then Return
    
        'This is our text that we'd like to append to the end of the string
        Dim RepText = "PPP"
        'This is our text that we'll slowly take off the last character of until it fits
        Dim NewText As String = e.Value
    
        'Loop until our text fits. NOTE: You may have to take into account cell padding here, too
        Do While TextRenderer.MeasureText(NewText & RepText, C.InheritedStyle.Font).Width >= C.Size.Width
            'Chop the last character off and repeat
            NewText = NewText.Substring(0, NewText.Length - 2)
        Loop
    
        'Set the new cell's value
        e.Value = NewText & RepText
        'Flag that we've changed the cell's text
        e.FormattingApplied = True
    End Sub
    

    【讨论】:

      【解决方案2】:

      哇,效果很好,谢谢:)

      现在 DGV 中有一些列有英文字体。 所以,我插入了一些代码如下,不要格式化某些特定的单元格

       'Grab the current cell
      
       Select Case e.ColumnIndex
       Case 0, 2, 3, 4, 14, 16, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28
        Return
       End Select
      
       Dim C = DirectCast(sender, DataGridView).Rows(e.RowIndex).Cells(e.ColumnIndex)
      

      【讨论】:

        最近更新 更多