DataGridView.RowPrePaint Event


DataGridViewRow가 칠해지기 전에 발생합니다.Occurs before a DataGridViewRow is painted.

 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 


다음 코드 예제에서는 RowPrePaint 이벤트에 대 한 처리기를 사용 하 여 행이 선택 된 경우 그라데이션 행 배경을 칠하는 방법을 보여 줍니다.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. 이 예제는에서 사용할 수 있는 보다 큰 예제의 일부 방법: Windows Forms DataGridView 컨트롤에서 행 모양 사용자 지정합니다.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) ==
        // Calculate the bounds of the row.
        Rectangle rowBounds = new Rectangle(
            this.dataGridView1.RowHeadersWidth, e.RowBounds.Top,
                DataGridViewElementStates.Visible) -
            this.dataGridView1.HorizontalScrollingOffset + 1,

        // Paint the custom selection background.
        using (Brush backbrush =
            new System.Drawing.Drawing2D.LinearGradientBrush(rowBounds,
            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, _

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

End Sub


이 이벤트를 단독으로 처리 하거나 RowPostPaint 이벤트와 함께 처리 하 여 컨트롤에서 행의 모양을 사용자 지정할 수 있습니다.You can handle this event alone or in combination with the RowPostPaint event to customize the appearance of rows in the control. 전체 행을 직접 그리거나 특정 행의 특정 부분을 페인트 하 고 DataGridViewRowPrePaintEventArgs 클래스의 다음 메서드를 사용 하 여 다른 부분을 그릴 수 있습니다.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:

VisualStyleRenderer 클래스를 사용 하 여 현재 테마를 사용 하 여 표준 컨트롤을 그릴 수도 있습니다.You can also use the VisualStyleRenderer class to paint standard controls using the current theme. 자세한 내용은 비주얼 스타일을 사용 하 여 컨트롤 렌더링합니다.For more information, see Rendering Controls with Visual Styles. Visual Studio 2005Visual Studio 2005를 사용 하는 경우 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.

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.For more information about how to handle events, see Handling and Raising Events.

Applies to

See also