DataGridView.RowPrePaint Evento

Definición

Se produce antes de que se dibuje un objeto DataGridViewRow.Occurs before a DataGridViewRow is painted.

public:
 event System::Windows::Forms::DataGridViewRowPrePaintEventHandler ^ RowPrePaint;
public event System.Windows.Forms.DataGridViewRowPrePaintEventHandler RowPrePaint;
member this.RowPrePaint : System.Windows.Forms.DataGridViewRowPrePaintEventHandler 
Public Custom Event RowPrePaint As DataGridViewRowPrePaintEventHandler 

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar un controlador para el evento RowPrePaint para pintar un fondo de fila de degradado si se selecciona la fila.The following code example demonstrates how to use a handler for the RowPrePaint event to paint a gradient row background if the row is selected. Este ejemplo forma parte de un ejemplo más grande disponible en [How para: Personalice la apariencia de las filas en el control DataGridView Windows Forms @ no__t-0.This example is part of a larger example available in How to: Customize the Appearance of Rows in the Windows Forms DataGridView Control.

// 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);
        }
    }
}
' Paints the custom selection background for selected rows.
Sub dataGridView1_RowPrePaint(ByVal sender As Object, _
    ByVal e As DataGridViewRowPrePaintEventArgs) _
    Handles dataGridView1.RowPrePaint

    ' Do not automatically paint the focus rectangle.
    e.PaintParts = e.PaintParts And Not DataGridViewPaintParts.Focus

    ' Determine whether the cell should be painted with the 
    ' custom selection background.
    If (e.State And DataGridViewElementStates.Selected) = _
        DataGridViewElementStates.Selected Then

        ' Calculate the bounds of the row.
        Dim rowBounds As New Rectangle( _
            Me.dataGridView1.RowHeadersWidth, e.RowBounds.Top, _
            Me.dataGridView1.Columns.GetColumnsWidth( _
            DataGridViewElementStates.Visible) - _
            Me.dataGridView1.HorizontalScrollingOffset + 1, _
            e.RowBounds.Height)

        ' Paint the custom selection background.
        Dim backbrush As New _
            System.Drawing.Drawing2D.LinearGradientBrush(rowBounds, _
            Me.dataGridView1.DefaultCellStyle.SelectionBackColor, _
            e.InheritedRowStyle.ForeColor, _
            System.Drawing.Drawing2D.LinearGradientMode.Horizontal)
        Try
            e.Graphics.FillRectangle(backbrush, rowBounds)
        Finally
            backbrush.Dispose()
        End Try
    End If

End Sub

Comentarios

Puede controlar este evento por sí solo o en combinación con el evento RowPostPaint para personalizar la apariencia de las filas del control.You can handle this event alone or in combination with the RowPostPaint event to customize the appearance of rows in the control. Puede pintar las filas enteras usted mismo, o bien pintar partes específicas de las filas y usar los métodos siguientes de la clase DataGridViewRowPrePaintEventArgs para pintar otras partes:You can paint entire rows yourself, or paint specific parts of rows and use the following methods of the DataGridViewRowPrePaintEventArgs class to paint other parts:

También puede usar la clase VisualStyleRenderer para pintar controles estándar con el tema actual.You can also use the VisualStyleRenderer class to paint standard controls using the current theme. Para obtener más información, consulte Representar controles con estilos visuales.For more information, see Rendering Controls with Visual Styles. Si usa Visual Studio 2005Visual Studio 2005, también tiene acceso a una gran biblioteca de imágenes estándar que puede usar con el control DataGridView.If you are using Visual Studio 2005Visual Studio 2005, you also have access to a large library of standard images that you can use with the DataGridView control.

Para obtener más información acerca de cómo controlar eventos, vea controlar y provocar eventos.For more information about how to handle events, see Handling and Raising Events.

Se aplica a

Consulte también: