DataGridView-Ereignisse


.NET Framework-Klassenbibliothek
DataGridView.CellPainting-Ereignis

Hinweis: Dieses Ereignis ist neu in .NET Framework, Version 2.0.

Tritt ein, wenn eine Zelle gezeichnet werden muss.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

Visual Basic (Deklaration)
Public Event CellPainting As DataGridViewCellPaintingEventHandler
Visual Basic (Verwendung)
Dim instance As DataGridView
Dim handler As DataGridViewCellPaintingEventHandler

AddHandler instance.CellPainting, handler
C#
public event DataGridViewCellPaintingEventHandler CellPainting
C++
public:
event DataGridViewCellPaintingEventHandler^ CellPainting {
    void add (DataGridViewCellPaintingEventHandler^ value);
    void remove (DataGridViewCellPaintingEventHandler^ value);
}
J#
/** @event */
public void add_CellPainting (DataGridViewCellPaintingEventHandler value)

/** @event */
public void remove_CellPainting (DataGridViewCellPaintingEventHandler value)
JScript
JScript unterstützt die Verwendung von Ereignissen, aber nicht die Deklaration von neuen Ereignissen.
Hinweise

Sie können dieses Ereignis behandeln, um die Darstellung von Zellen im Steuerelement anzupassen. Sie können gesamte Zellen oder nur bestimmte Teile von Zellen selbst zeichnen und die anderen Teile mit der DataGridViewCellPaintingEventArgs.PaintBackground-Methode oder der DataGridViewCellPaintingEventArgs.PaintContent-Methode zeichnen. Sie können auch die VisualStyleRenderer-Klasse zum Zeichnen von Standardsteuerelementen mit dem aktuellen Design verwenden. Weitere Informationen finden Sie unter Rendering von Steuerelementen mit visuellen Stilen. Wenn Sie Visual Studio 2005 verwenden, haben Sie außerdem Zugriff auf eine umfassende Bibliothek der Standardbilder, die Sie mit dem DataGridView-Steuerelement verwenden können.

Weitere Informationen finden Sie unter Visual Studio 2005 Bildbibliothek.

Greifen Sie beim Behandeln dieses Ereignisses über die Parameter des Ereignishandlers auf die Zelle zu, nicht direkt auf die Zelle.

Weitere Informationen über die Behandlung von Ereignissen finden Sie unter Behandeln von Ereignissen.

Beispiel

Im folgenden Codebeispiel wird die Verwendung dieses Ereignisses veranschaulicht, um die Darstellung aller Zellen in einer bestimmten Spalte anzupassen.

Dieses Beispiel ist Teil eines umfangreicheren in Gewusst wie: Anpassen der Darstellung von Zellen im DataGridView-Steuerelement von Windows Forms verfügbaren Beispiels.

Visual Basic
Private Sub dataGridView1_CellPainting(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
    Handles dataGridView1.CellPainting

    If Me.dataGridView1.Columns("ContactName").Index = _
        e.ColumnIndex AndAlso e.RowIndex >= 0 Then

        Dim newRect As New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, _
            e.CellBounds.Width - 4, e.CellBounds.Height - 4)
        Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor)
        Dim gridBrush As New SolidBrush(Me.dataGridView1.GridColor)
        Dim gridLinePen As New Pen(gridBrush)

        Try

            ' Erase the cell.
            e.Graphics.FillRectangle(backColorBrush, e.CellBounds)

            ' Draw the grid lines (only the right and bottom lines;
            ' DataGridView takes care of the others).
            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
                e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
                e.CellBounds.Bottom - 1)
            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
                e.CellBounds.Top, e.CellBounds.Right - 1, _
                e.CellBounds.Bottom)

            ' Draw the inset highlight box.
            e.Graphics.DrawRectangle(Pens.Blue, newRect)

            ' Draw the text content of the cell, ignoring alignment.
            If Not (e.Value Is Nothing) Then
                e.Graphics.DrawString(CStr(e.Value), e.CellStyle.Font, _
                Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, _
                StringFormat.GenericDefault)
            End If
            e.Handled = True

        Finally
            gridLinePen.Dispose()
            gridBrush.Dispose()
            backColorBrush.Dispose()
        End Try

    End If

End Sub
C#
private void dataGridView1_CellPainting(object sender,
System.Windows.Forms.DataGridViewCellPaintingEventArgs e)
{
    if (this.dataGridView1.Columns["ContactName"].Index ==
        e.ColumnIndex && e.RowIndex >= 0)
    {
        Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
            e.CellBounds.Y + 1, e.CellBounds.Width - 4,
            e.CellBounds.Height - 4);

        using (
            Brush gridBrush = new SolidBrush(this.dataGridView1.GridColor),
            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
        {
            using (Pen gridLinePen = new Pen(gridBrush))
            {
                // Erase the cell.
                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                // Draw the grid lines (only the right and bottom lines;
                // DataGridView takes care of the others).
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                    e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                    e.CellBounds.Bottom - 1);
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                    e.CellBounds.Top, e.CellBounds.Right - 1,
                    e.CellBounds.Bottom);

                // Draw the inset highlight box.
                e.Graphics.DrawRectangle(Pens.Blue, newRect);

                // Draw the text content of the cell, ignoring alignment.
                if (e.Value != null)
                {
                    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                        Brushes.Crimson, e.CellBounds.X + 2,
                        e.CellBounds.Y + 2, StringFormat.GenericDefault);
                }
                e.Handled = true;
            }
        }
    }
}
Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0
Siehe auch

Page view tracker