DataGridView.RowPrePaint Evento

Definición

Se produce antes de que se dibuje un objeto DataGridViewRow.

C#
public event System.Windows.Forms.DataGridViewRowPrePaintEventHandler RowPrePaint;
C#
public event System.Windows.Forms.DataGridViewRowPrePaintEventHandler? RowPrePaint;

Tipo de evento

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar un controlador para el RowPrePaint evento para pintar un fondo de fila degradado si se selecciona la fila. Este ejemplo forma parte de un ejemplo más grande disponible en How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control.

C#
// Paints the custom selection background for selected rows.
void dataGridView1_RowPrePaint(object sender,
        DataGridViewRowPrePaintEventArgs e)
{
    // Do not automatically paint the focus rectangle.
    e.PaintParts &= ~DataGridViewPaintParts.Focus;

    // Determine whether the cell should be painted
    // with the custom selection background.
    if ((e.State & DataGridViewElementStates.Selected) ==
                DataGridViewElementStates.Selected)
    {
        // Calculate the bounds of the row.
        Rectangle rowBounds = new Rectangle(
            this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
            this.dataGridView1.Columns.GetColumnsWidth(
                DataGridViewElementStates.Visible) -
            this.dataGridView1.HorizontalScrollingOffset + 1,
            e.RowBounds.Height);

        // Paint the custom selection background.
        using (Brush backbrush =
            new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
                this.dataGridView1.DefaultCellStyle.SelectionBackColor,
                e.InheritedRowStyle.ForeColor,
                System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(backbrush, rowBounds);
        }
    }
}

Comentarios

Puede controlar este evento solo o en combinación con el RowPostPaint evento para personalizar la apariencia de las filas en el control. Puede pintar filas enteras usted mismo, o pintar partes específicas de filas y usar los métodos siguientes de la DataGridViewRowPrePaintEventArgs clase para pintar otras partes:

También puede usar la VisualStyleRenderer clase para pintar controles estándar mediante el tema actual. Para obtener más información, consulte Representar controles con estilos visuales. Si usa Visual Studio 2005, también tiene acceso a una biblioteca grande de imágenes estándar que puede usar con el DataGridView control .

Para obtener más información acerca de cómo controlar eventos, vea controlar y provocar eventos.

Se aplica a

Producto Versiones
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Consulte también