DataGridView.RowPrePaint 이벤트

정의

DataGridViewRow가 칠해지기 전에 발생합니다.

public:
 event System::Windows::Forms::DataGridViewRowPrePaintEventHandler ^ RowPrePaint;
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 

이벤트 유형

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 행이 선택 된 경우 그라데이션 행 배경을 그리기 위해 이벤트에 대 RowPrePaint 한 처리기를 사용 합니다. 이 예제는에서 사용할 수 있는 보다 큰 예제의 일부 방법: Windows Forms DataGridView 컨트롤에서 행 모양 사용자 지정합니다.

// 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

설명

이 이벤트를 단독으로 처리하거나 이벤트와 함께 RowPostPaint 처리하여 컨트롤의 행 모양을 사용자 지정할 수 있습니다. 전체 행을 직접 그리거나 행의 특정 부분을 칠하고 클래스의 DataGridViewRowPrePaintEventArgs 다음 메서드를 사용하여 다른 부분을 그릴 수 있습니다.

클래스를 VisualStyleRenderer 사용하여 현재 테마를 사용하여 표준 컨트롤을 그릴 수도 있습니다. 자세한 내용은 비주얼 스타일을 사용 하 여 컨트롤 렌더링합니다. Visual Studio 2005를 사용하는 경우 컨트롤과 함께 사용할 수 있는 표준 이미지의 큰 라이브러리에 DataGridView 액세스할 수도 있습니다.

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

적용 대상

추가 정보