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
VB.NET Version (online übersetzt)
Spoiler anzeigen
LG,
Bruno
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
C-Quellcode
- /// <summary>
- /// Draw first col with path ellipsis
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void dgvFiles_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
- {
- // Paint not in row/col header
- if (e.RowIndex == -1 || e.ColumnIndex == -1) return;
- // first col
- if (e.ColumnIndex == 0)
- {
- // Stringformat
- StringFormat sf = new StringFormat();
- sf.LineAlignment = StringAlignment.Near;
- sf.FormatFlags = StringFormatFlags.NoWrap;
- sf.Trimming = StringTrimming.EllipsisPath;
- // Cell fill with backcolor
- e.Graphics.FillRectangle(new SolidBrush(e.CellStyle.BackColor), e.CellBounds);
- // Draw path-ellipsed text
- e.Graphics.DrawString(e.Value.ToString(), new Font(this.Font, FontStyle.Regular), new SolidBrush(e.CellStyle.ForeColor), e.CellBounds, sf);
- // Done!
- e.Handled = true;
- }
- // Paint selection bar
- if (dgvFiles.Rows[e.RowIndex].Cells[0].Selected)
- {
- // Cell fill with selection backcolor
- e.Graphics.FillRectangle(new SolidBrush(e.CellStyle.SelectionBackColor), e.CellBounds);
- // Paint all
- e.Paint(e.ClipBounds, (DataGridViewPaintParts.All & ~DataGridViewPaintParts.Background));
- // Done!
- e.Handled = true;
- }
- }
VB.NET Version (online übersetzt)
VB.NET-Quellcode
- ''' <summary>
- ''' Draw first col with path ellipsis
- ''' </summary>
- ''' <param name="sender"></param>
- ''' <param name="e"></param>
- Private Sub dgvFiles_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs)
- ' Paint not in row/col header
- If e.RowIndex = -1 OrElse e.ColumnIndex = -1 Then
- Return
- End If
- ' first col
- If e.ColumnIndex = 0 Then
- ' Stringformat
- Dim sf As New StringFormat()
- sf.LineAlignment = StringAlignment.Near
- sf.FormatFlags = StringFormatFlags.NoWrap
- sf.Trimming = StringTrimming.EllipsisPath
- ' Cell fill with backcolor
- e.Graphics.FillRectangle(New SolidBrush(e.CellStyle.BackColor), e.CellBounds)
- ' Draw path-ellipsed text
- e.Graphics.DrawString(e.Value.ToString(), New Font(Me.Font, FontStyle.Regular), New SolidBrush(e.CellStyle.ForeColor), e.CellBounds, sf)
- ' Done!
- e.Handled = True
- End If
- ' Paint selection bar
- If dgvFiles.Rows(e.RowIndex).Cells(0).Selected Then
- ' Cell fill with selection backcolor
- e.Graphics.FillRectangle(New SolidBrush(e.CellStyle.SelectionBackColor), e.CellBounds)
- ' Paint all
- e.Paint(e.ClipBounds, (DataGridViewPaintParts.All And Not DataGridViewPaintParts.Background))
- ' Done!
- e.Handled = True
- End If
- End Sub
LG,
Bruno