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

发布于 2024-10-08 07:14:21 字数 675 浏览 0 评论 0原文

我正在 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 链接上的解决方案很好,我可以从中删除多少代码和/或我需要更多代码才能使其完全工作?

I am developing a project in VB.Net and I am using Gujarati fonts (non-Unicode).

I have placed a DaraGridView (DGV) and displaying the data stored in database in DGV.

In DGV, if the contents of the cell is truncated, the DGV shows ellipses (three dots) (...); but as the font is set to a Gujarati font, it displays a Gujarati alphabet "ઈઈઈ" instead of "..." because the Gujarati character "ઈ" is coded with the English "." (dot) character.

In Gujarati font, "." can be generated with the English character "P".

So, How can I change the ellipses to English "P" character? OR How can I remove the ellipses completely?

I have found a similar solution by berc on Tuesday, August 31, 2010 1:33 PM :
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/95c8963f-9832-4e2d-b057-3590afe3b65d

but I am not sure it is the perfect and/or the only way to do so, AND it will really work or not.
If the solution on the above link of MSDN is fine, how much code from it I can remove and/or will I need more code to get it work fully?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的憧憬 2024-10-15 07:14:21

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

You can override the CellFormatting event:

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
乖乖兔^ω^ 2024-10-15 07:14:21

哇,效果很好,谢谢:)

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

Wow, it worked fine, thanks :)

Now there are some columns in DGV having English font.
So, I inserted some code as following to not to format some specific cells

 '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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文