2010-12-15 151 views
3

Je développe un projet en VB.Net et je suis en utilisant les polices gujarati (non Unicode).Changer les trois points (Ellipses) (...) à des caractères personnalisés dans VB.Net DataGridView lorsque le contenu cellulaire est tronquée

j'ai placé un DaraGridView (DGV) et afficher les données stockées dans la base de données en DGV.

Dans DGV, si le contenu de la cellule est tronqué, la DGV montre ellipses (trois points) (...); mais comme la police est définie sur une police gujarati, elle affiche un alphabet gujarati "ઈઈઈ" au lieu de "..." car le caractère gujarati "ઈ" est codé avec l'anglais "." (point) caractère.

Dans la police gujarati, "" peut être généré avec le caractère anglais "P".

Alors, comment puis-je changer les ellipses Anglais caractère « P »? OU Comment puis-je supprimer complètement les ellipses?

J'ai trouvé une solution similaire par Berc le mardi 31 Août, 2010 13:33: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/95c8963f-9832-4e2d-b057-3590afe3b65d

mais je ne suis pas sûr qu'il est parfait et/ou la seule façon de le faire, et ça va vraiment marcher ou pas. Si la solution sur le lien ci-dessus de MSDN est bien, combien de code de ce que je peux supprimer et/ou dois-je plus de code pour le faire fonctionner pleinement?

Répondre

3

Vous pouvez remplacer l'événement 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 
0

Wow, il a bien fonctionné, merci :)

Maintenant il y a des colonnes DGV ayant la police anglaise. Donc, j'ai inséré du code comme suit pour ne pas formater certaines cellules spécifiques

'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)