Compartilhar via


DataGridViewCellPaintingEventArgs Classe

Definição

Fornece dados para o evento CellPainting.

public ref class DataGridViewCellPaintingEventArgs : System::ComponentModel::HandledEventArgs
public class DataGridViewCellPaintingEventArgs : System.ComponentModel.HandledEventArgs
type DataGridViewCellPaintingEventArgs = class
    inherit HandledEventArgs
Public Class DataGridViewCellPaintingEventArgs
Inherits HandledEventArgs
Herança
DataGridViewCellPaintingEventArgs

Exemplos

O exemplo de código a seguir ilustra o uso desse tipo. Para obter mais informações, consulte Como personalizar a aparência das células no controle DataGridView dos 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

Comentários

O CellPainting evento é gerado para cada DataGridViewCell um que está visível em um DataGridView. Para melhorar o desempenho, defina as propriedades em um DataGridViewCellPaintingEventArgs para alterar a aparência da célula em vez de acessar diretamente uma célula na DataGridView. Se você pintar manualmente a célula, defina a HandledEventArgs.Handled propriedade como true. Se você não definir HandledEventArgs.Handled como true, a célula pintará sobre suas personalizações.

Construtores

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

Inicializa uma nova instância da classe DataGridViewCellPaintingEventArgs.

Propriedades

Nome Description
AdvancedBorderStyle

Obtém o estilo de borda do atual DataGridViewCell.

CellBounds

Obter os limites do atual DataGridViewCell.

CellStyle

Obtém o estilo de célula do atual DataGridViewCell.

ClipBounds

Obtém a área do DataGridView que precisa ser repintado.

ColumnIndex

Obtém o índice de coluna do atual DataGridViewCell.

ErrorText

Obtém uma cadeia de caracteres que representa uma mensagem de erro para o atual DataGridViewCell.

FormattedValue

Obtém o valor formatado do atual DataGridViewCell.

Graphics

Obtém o Graphics usado para pintar a corrente DataGridViewCell.

Handled

Obtém ou define um valor que indica se o manipulador de eventos lidou completamente com o evento ou se o sistema deve continuar seu próprio processamento.

(Herdado de HandledEventArgs)
PaintParts

As partes da célula que devem ser pintadas.

RowIndex

Obtém o índice de linha do atual DataGridViewCell.

State

Obtém o estado do atual DataGridViewCell.

Value

Obtém o valor do atual DataGridViewCell.

Métodos

Nome Description
Equals(Object)

Determina se o objeto especificado é igual ao objeto atual.

(Herdado de Object)
GetHashCode()

Serve como a função de hash padrão.

(Herdado de Object)
GetType()

Obtém o Type da instância atual.

(Herdado de Object)
MemberwiseClone()

Cria uma cópia superficial do Objectatual.

(Herdado de Object)
Paint(Rectangle, DataGridViewPaintParts)

Pinta as partes especificadas da célula para a área nos limites especificados.

PaintBackground(Rectangle, Boolean)

Pinta a tela de fundo da célula para a área nos limites especificados.

PaintContent(Rectangle)

Pinta o conteúdo da célula para a área nos limites especificados.

ToString()

Retorna uma cadeia de caracteres que representa o objeto atual.

(Herdado de Object)

Aplica-se a

Confira também