Partage via


DataGridViewCellPaintingEventArgs Classe

Définition

Fournit des données pour l'événement CellPainting.

public ref class DataGridViewCellPaintingEventArgs : System::ComponentModel::HandledEventArgs
public class DataGridViewCellPaintingEventArgs : System.ComponentModel.HandledEventArgs
type DataGridViewCellPaintingEventArgs = class
    inherit HandledEventArgs
Public Class DataGridViewCellPaintingEventArgs
Inherits HandledEventArgs
Héritage
DataGridViewCellPaintingEventArgs

Exemples

L’exemple de code suivant illustre l’utilisation de ce type. Pour plus d’informations, consultez Guide pratique pour personnaliser l’apparence des cellules dans le contrôle DataGridView Windows Forms.

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;
            }
        }
    }
}
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 (e.Value IsNot 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

Remarques

L’événement CellPainting est déclenché pour chaque DataGridViewCell élément visible sur un DataGridView. Pour améliorer les performances, définissez les propriétés dans un DataGridViewCellPaintingEventArgs pour modifier l’apparence de la cellule au lieu d’accéder directement à une cellule dans le DataGridView. Si vous peindrez manuellement la cellule, définissez la HandledEventArgs.Handled propriété sur true. Si vous ne définissez HandledEventArgs.Handledtruepas la valeur , la cellule peint sur vos personnalisations.

Constructeurs

Nom Description
DataGridViewCellPaintingEventArgs(DataGridView, Graphics, Rectangle, Rectangle, Int32, Int32, DataGridViewElementStates, Object, Object, String, DataGridViewCellStyle, DataGridViewAdvancedBorderStyle, DataGridViewPaintParts)

Initialise une nouvelle instance de la classe DataGridViewCellPaintingEventArgs.

Propriétés

Nom Description
AdvancedBorderStyle

Obtient le style de bordure du fichier actuel DataGridViewCell.

CellBounds

Obtenez les limites du fichier actuel DataGridViewCell.

CellStyle

Obtient le style de cellule du fichier actif DataGridViewCell.

ClipBounds

Obtient la zone de l’élément DataGridView qui doit être repeinte.

ColumnIndex

Obtient l’index de colonne du fichier actif DataGridViewCell.

ErrorText

Obtient une chaîne qui représente un message d’erreur pour le message d’erreur actuel DataGridViewCell.

FormattedValue

Obtient la valeur mise en forme du fichier actif DataGridViewCell.

Graphics

Obtient l’utilisé Graphics pour peindre le courant DataGridViewCell.

Handled

Obtient ou définit une valeur qui indique si le gestionnaire d’événements a complètement géré l’événement ou si le système doit continuer son propre traitement.

(Hérité de HandledEventArgs)
PaintParts

Parties de cellule à peindre.

RowIndex

Obtient l’index de ligne du fichier actif DataGridViewCell.

State

Obtient l’état du fichier actif DataGridViewCell.

Value

Obtient la valeur du fichier actif DataGridViewCell.

Méthodes

Nom Description
Equals(Object)

Détermine si l’objet spécifié est égal à l’objet actuel.

(Hérité de Object)
GetHashCode()

Sert de fonction de hachage par défaut.

(Hérité de Object)
GetType()

Obtient la Type de l’instance actuelle.

(Hérité de Object)
MemberwiseClone()

Crée une copie superficielle du Objectactuel.

(Hérité de Object)
Paint(Rectangle, DataGridViewPaintParts)

Peint les parties spécifiées de la cellule pour la zone dans les limites spécifiées.

PaintBackground(Rectangle, Boolean)

Peint l’arrière-plan de la cellule pour la zone dans les limites spécifiées.

PaintContent(Rectangle)

Peint le contenu de cellule de la zone dans les limites spécifiées.

ToString()

Retourne une chaîne qui représente l’objet actuel.

(Hérité de Object)

S’applique à

Voir aussi