DataGridView.RowHeadersWidth Proprietà

Definizione

Ottiene o imposta la larghezza, in pixel, della colonna contenente le intestazioni di riga.

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

Valore della proprietà

Int32

Larghezza, in pixel, della colonna contenente le intestazioni di riga. Il valore predefinito è 43.

Eccezioni

Il valore specificato durante l'impostazione di questa proprietà è inferiore alla larghezza minima di 4 pixel o è superiore alla larghezza massima di 32768 pixel.

Esempio

Nell'esempio di codice seguente viene illustrato come usare la RowHeadersWidth proprietà in uno scenario di disegno di riga. Nell'esempio viene usato il valore di questa proprietà per calcolare i limiti in cui viene disegnato uno sfondo personalizzato.

Questo codice fa parte di un esempio più grande disponibile inHow to: Personalizzare l'aspetto delle righe nel controllo DataGridView Windows Forms.

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

Commenti

La RowHeadersWidth proprietà può essere usata per ridimensionare la colonna dell'intestazione di riga in una larghezza specificata. Per regolare la larghezza di questa colonna per adattare il contenuto delle celle dell'intestazione di riga, usare il AutoResizeRowHeadersWidth metodo .

Si applica a

Vedi anche