当单元格内容被截断时,将三个点(省略号)(...)更改为VB.Net DataGridView中的自定义字符

时间:2010-12-15 22:21:54

标签: vb.net datagridview cell truncate

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

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

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

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

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

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

但我不确定这是完美的和/或唯一的方法,它会真的起作用。 如果MSDN的上述链接上的解决方案没问题,我可以删除多少代码和/或我需要更多代码才能使其完全正常工作?

2 个答案:

答案 0 :(得分:3)

您可以覆盖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

答案 1 :(得分:0)

哇,它工作正常,谢谢:)。

现在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)