问题
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?
回答1:
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
回答2:
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)
来源:https://stackoverflow.com/questions/4455564/change-the-three-dots-ellipses-to-custom-characters-in-vb-net-datagridvi