DataGridView CellPainting Event // Path-Ellipsis Content in Cell

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von diylab.

    DataGridView CellPainting Event // Path-Ellipsis Content in Cell

    Hallo Leute,

    für mein DataGridView brauche ich in der ersten Spalte ein s.g. "Path-Ellipsis".
    Ihr wisst schon - wenn ein Pfad angezeigt werden soll und Dieser nicht in die Anzeigebreite passt, wird in der Mitte gekürzt und dafür "Pünktchen" angezeigt..

    Die zu sehende Methode macht genau das, was ich möchte - allerdings im CellPainting Event.

    Meine Frage(n):
    Ist dieser Weg der Richtige?
    Oder kann man die Stringformate vielleicht doch anders zuweisen?
    Vielleicht denke ich ja zu kompliziert?


    C# Version
    Spoiler anzeigen

    C-Quellcode

    1. /// <summary>
    2. /// Draw first col with path ellipsis
    3. /// </summary>
    4. /// <param name="sender"></param>
    5. /// <param name="e"></param>
    6. private void dgvFiles_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
    7. {
    8. // Paint not in row/col header
    9. if (e.RowIndex == -1 || e.ColumnIndex == -1) return;
    10. // first col
    11. if (e.ColumnIndex == 0)
    12. {
    13. // Stringformat
    14. StringFormat sf = new StringFormat();
    15. sf.LineAlignment = StringAlignment.Near;
    16. sf.FormatFlags = StringFormatFlags.NoWrap;
    17. sf.Trimming = StringTrimming.EllipsisPath;
    18. // Cell fill with backcolor
    19. e.Graphics.FillRectangle(new SolidBrush(e.CellStyle.BackColor), e.CellBounds);
    20. // Draw path-ellipsed text
    21. e.Graphics.DrawString(e.Value.ToString(), new Font(this.Font, FontStyle.Regular), new SolidBrush(e.CellStyle.ForeColor), e.CellBounds, sf);
    22. // Done!
    23. e.Handled = true;
    24. }
    25. // Paint selection bar
    26. if (dgvFiles.Rows[e.RowIndex].Cells[0].Selected)
    27. {
    28. // Cell fill with selection backcolor
    29. e.Graphics.FillRectangle(new SolidBrush(e.CellStyle.SelectionBackColor), e.CellBounds);
    30. // Paint all
    31. e.Paint(e.ClipBounds, (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background));
    32. // Done!
    33. e.Handled = true;
    34. }
    35. }


    VB.NET Version (online übersetzt)
    Spoiler anzeigen

    VB.NET-Quellcode

    1. ''' <summary>
    2. ''' Draw first col with path ellipsis
    3. ''' </summary>
    4. ''' <param name="sender"></param>
    5. ''' <param name="e"></param>
    6. Private Sub dgvFiles_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs)
    7. ' Paint not in row/col header
    8. If e.RowIndex = -1 OrElse e.ColumnIndex = -1 Then
    9. Return
    10. End If
    11. ' first col
    12. If e.ColumnIndex = 0 Then
    13. ' Stringformat
    14. Dim sf As New StringFormat()
    15. sf.LineAlignment = StringAlignment.Near
    16. sf.FormatFlags = StringFormatFlags.NoWrap
    17. sf.Trimming = StringTrimming.EllipsisPath
    18. ' Cell fill with backcolor
    19. e.Graphics.FillRectangle(New SolidBrush(e.CellStyle.BackColor), e.CellBounds)
    20. ' Draw path-ellipsed text
    21. e.Graphics.DrawString(e.Value.ToString(), New Font(Me.Font, FontStyle.Regular), New SolidBrush(e.CellStyle.ForeColor), e.CellBounds, sf)
    22. ' Done!
    23. e.Handled = True
    24. End If
    25. ' Paint selection bar
    26. If dgvFiles.Rows(e.RowIndex).Cells(0).Selected Then
    27. ' Cell fill with selection backcolor
    28. e.Graphics.FillRectangle(New SolidBrush(e.CellStyle.SelectionBackColor), e.CellBounds)
    29. ' Paint all
    30. e.Paint(e.ClipBounds, (DataGridViewPaintParts.All And Not DataGridViewPaintParts.Background))
    31. ' Done!
    32. e.Handled = True
    33. End If
    34. End Sub



    LG,
    Bruno