DataGridView.RowHeadersWidth Property

Definition

Gets or sets the width, in pixels, of the column that contains the row headers.

public int RowHeadersWidth { get; set; }

Property Value

The width, in pixels, of the column that contains row headers. The default is 43.

Exceptions

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.

Examples

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.

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);
        }
    }
}

Remarks

The RowHeadersWidth property can be used to resize the row header column to a specified width. To adjust the width of this column to fit the contents of the row header cells, use the AutoResizeRowHeadersWidth method.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also