DataGridView.RowHeadersWidth DataGridView.RowHeadersWidth DataGridView.RowHeadersWidth DataGridView.RowHeadersWidth Property

定義

行ヘッダーを含む列の幅をピクセル単位で取得または設定します。Gets or sets the width, in pixels, of the column that contains the row headers.

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

プロパティ値

行ヘッダーを含む列の幅 (ピクセル単位)。The width, in pixels, of the column that contains row headers. 既定値は 43 です。The default is 43.

例外

このプロパティを設定するときに指定された値が、最小幅 4 ピクセルより小さいか、最大幅 32768 ピクセルよりも大きくなっています。The specified value when setting this property is less than the minimum width of 4 pixels or is greater than the maximum width of 32768 pixels.

次のコード例は、行描画シナリオRowHeadersWidthでプロパティを使用する方法を示しています。The following code example illustrates how to use the RowHeadersWidth property in a row-painting scenario. この例では、このプロパティの値を使用して、カスタム背景の描画範囲を計算します。In the example, the value of this property is used to calculate the bounds within which a custom background is drawn.

このコードは、次の方法で使用できる大きな例の一部です。Windows フォーム DataGridView コントロールの行の外観をカスタマイズします。This code is part of a larger example available inHow 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

注釈

RowHeadersWidthプロパティを使用して、行ヘッダー列のサイズを指定した幅に変更できます。The RowHeadersWidth property can be used to resize the row header column to a specified width. 行ヘッダーセルの内容に合わせてこの列の幅を調整するには、 AutoResizeRowHeadersWidthメソッドを使用します。To adjust the width of this column to fit the contents of the row header cells, use the AutoResizeRowHeadersWidth method.

適用対象

こちらもご覧ください