DataGridView.RowHeadersWidth 속성

정의

행 머리글이 포함된 열의 너비(픽셀)를 가져오거나 설정합니다.

public:
 property int RowHeadersWidth { int get(); void set(int value); };
public int RowHeadersWidth { get; set; }
member this.RowHeadersWidth : int with get, set
Public Property RowHeadersWidth As Integer

속성 값

Int32

행 머리글이 포함된 열의 너비(픽셀)입니다. 기본값은 43입니다.

예외

이 속성을 설정할 때 지정된 값이 최소 너비인 4픽셀보다 작거나 최대 너비인 32768픽셀보다 큰 경우

예제

다음 코드 예제에서는 행 그리기 시나리오에서 속성을 사용 RowHeadersWidth 하는 방법을 보여 줍니다. 이 예제에서 이 속성의 값은 사용자 지정 배경이 그려지는 범위를 계산하는 데 사용됩니다.

이 코드는 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

설명

RowHeadersWidth 속성을 사용하여 행 머리글 열의 크기를 지정된 너비로 조정할 수 있습니다. 행 머리글 셀의 내용에 맞게 이 열의 너비를 조정하려면 이 메서드를 AutoResizeRowHeadersWidth 사용합니다.

적용 대상

추가 정보