DataGridView.CellPainting DataGridView.CellPainting DataGridView.CellPainting DataGridView.CellPainting Event

정의

셀을 그려야 할 때 발생합니다.Occurs when a cell needs to be drawn.

public:
 event System::Windows::Forms::DataGridViewCellPaintingEventHandler ^ CellPainting;
public event System.Windows.Forms.DataGridViewCellPaintingEventHandler CellPainting;
member this.CellPainting : System.Windows.Forms.DataGridViewCellPaintingEventHandler 
Public Custom Event CellPainting As DataGridViewCellPaintingEventHandler 

예제

다음 코드 예제에는 특정 열의 모든 셀의 모양을 사용자 지정 하려면이 이벤트를 사용 하는 방법을 보여 줍니다.The following code example illustrates how to use this event to customize the appearance of all cells in a particular column.

이 코드는에서 사용할 수 있는 보다 큰 예제의 일부 방법: Windows Forms DataGridView 컨트롤에서 셀 모양 사용자 지정합니다.This code is part of a larger example available in How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control.

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

설명

컨트롤에서 셀 모양 사용자 지정 하려면이 이벤트를 처리할 수 있습니다.You can handle this event to customize the appearance of cells in the control. 전체 셀을 직접 그릴 셀의 특정 부분을 사용 합니다 DataGridViewCellPaintingEventArgs.PaintBackground 또는 DataGridViewCellPaintingEventArgs.PaintContent 다른 부분을 그리는 방법입니다.You can paint entire cells yourself, or paint specific parts of cells and use the DataGridViewCellPaintingEventArgs.PaintBackground or DataGridViewCellPaintingEventArgs.PaintContent methods 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.

이 이벤트를 처리할 때 셀에 직접 액세스 하는 대신 이벤트 처리기의 매개 변수를 통해 셀에 액세스 해야 있습니다.When handling this event, you should access the cell through the parameters of the event handler, rather than access the cell directly.

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

적용 대상

추가 정보