DataGridViewCellPaintingEventArgs Klass

Definition

Innehåller data för CellPainting händelsen.

public ref class DataGridViewCellPaintingEventArgs : System::ComponentModel::HandledEventArgs
public class DataGridViewCellPaintingEventArgs : System.ComponentModel.HandledEventArgs
type DataGridViewCellPaintingEventArgs = class
    inherit HandledEventArgs
Public Class DataGridViewCellPaintingEventArgs
Inherits HandledEventArgs
Arv
DataGridViewCellPaintingEventArgs

Exempel

Följande kodexempel illustrerar användningen av den här typen. För mer information, se Hur man: Anpassar utseendet på celler i Windows Forms DataGridView-kontrollen.

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

Kommentarer

Händelsen CellPainting aktiveras för varje DataGridViewCell som visas på en DataGridView. För att förbättra prestandan anger du egenskaperna i a DataGridViewCellPaintingEventArgs för att ändra cellens utseende i stället för att komma åt en cell direkt i DataGridView. Om du målar cellen manuellt anger du HandledEventArgs.Handled egenskapen till true. Om du inte anger HandledEventArgs.Handled till truekommer cellen att måla över dina anpassningar.

Konstruktorer

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

Initierar en ny instans av DataGridViewCellPaintingEventArgs klassen.

Egenskaper

Name Description
AdvancedBorderStyle

Hämtar kantlinjeformatet för den aktuella DataGridViewCell.

CellBounds

Hämta gränserna för den aktuella DataGridViewCell.

CellStyle

Hämtar cellformatet för den aktuella DataGridViewCell.

ClipBounds

Hämtar området för DataGridView det som måste målas om.

ColumnIndex

Hämtar kolumnindexet för den aktuella DataGridViewCell.

ErrorText

Hämtar en sträng som representerar ett felmeddelande för den aktuella DataGridViewCell.

FormattedValue

Hämtar det formaterade värdet för den aktuella DataGridViewCell.

Graphics

Hämtar den Graphics som används för att måla den aktuella DataGridViewCell.

Handled

Hämtar eller anger ett värde som anger om händelsehanteraren har hanterat händelsen helt eller om systemet ska fortsätta sin egen bearbetning.

(Ärvd från HandledEventArgs)
PaintParts

Celldelarna som ska målas.

RowIndex

Hämtar radindexet för den aktuella DataGridViewCell.

State

Hämtar tillståndet för den aktuella DataGridViewCell.

Value

Hämtar värdet för den aktuella DataGridViewCell.

Metoder

Name Description
Equals(Object)

Avgör om det angivna objektet är lika med det aktuella objektet.

(Ärvd från Object)
GetHashCode()

Fungerar som standard-hash-funktion.

(Ärvd från Object)
GetType()

Hämtar den aktuella instansen Type .

(Ärvd från Object)
MemberwiseClone()

Skapar en ytlig kopia av den aktuella Object.

(Ärvd från Object)
Paint(Rectangle, DataGridViewPaintParts)

Målar de angivna delarna av cellen för området inom de angivna gränserna.

PaintBackground(Rectangle, Boolean)

Målar cellbakgrunden för området inom de angivna gränserna.

PaintContent(Rectangle)

Målar cellinnehållet för området inom de angivna gränserna.

ToString()

Returnerar en sträng som representerar det aktuella objektet.

(Ärvd från Object)

Gäller för

Se även