DataGridView.RowHeadersDefaultCellStyle Propiedad

Definición

Obtiene o establece el estilo predeterminado aplicado a las celdas de encabezado de fila.

C#
public System.Windows.Forms.DataGridViewCellStyle RowHeadersDefaultCellStyle { get; set; }

Valor de propiedad

DataGridViewCellStyle que representa el estilo predeterminado aplicado a las celdas de encabezado de fila.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar esta propiedad en un DataGridView objeto con colores personalizados. Observe cómo se establece la DataGridViewCellStyle.SelectionBackColor propiedad en Color.Empty para que el valor se herede del DefaultCellStyle objeto .

Este ejemplo forma parte de un ejemplo más grande proporcionado en la información general de la DataGridViewCellStyle clase.

C#
// Configures the appearance and behavior of a DataGridView control.
private void InitializeDataGridView()
{
    // Initialize basic DataGridView properties.
    dataGridView1.Dock = DockStyle.Fill;
    dataGridView1.BackgroundColor = Color.LightGray;
    dataGridView1.BorderStyle = BorderStyle.Fixed3D;

    // Set property values appropriate for read-only display and 
    // limited interactivity. 
    dataGridView1.AllowUserToAddRows = false;
    dataGridView1.AllowUserToDeleteRows = false;
    dataGridView1.AllowUserToOrderColumns = true;
    dataGridView1.ReadOnly = true;
    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    dataGridView1.MultiSelect = false;
    dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
    dataGridView1.AllowUserToResizeColumns = false;
    dataGridView1.ColumnHeadersHeightSizeMode = 
        DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
    dataGridView1.AllowUserToResizeRows = false;
    dataGridView1.RowHeadersWidthSizeMode = 
        DataGridViewRowHeadersWidthSizeMode.DisableResizing;

    // Set the selection background color for all the cells.
    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White;
    dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;

    // Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
    // value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
    dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty;

    // Set the background color for all rows and for alternating rows. 
    // The value for alternating rows overrides the value for all rows. 
    dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray;
    dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray;

    // Set the row and column header styles.
    dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
    dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Black;
    dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Black;

    // Set the Format property on the "Last Prepared" column to cause
    // the DateTime to be formatted as "Month, Year".
    dataGridView1.Columns["Last Prepared"].DefaultCellStyle.Format = "y";

    // Specify a larger font for the "Ratings" column. 
    using (Font font = new Font(
        dataGridView1.DefaultCellStyle.Font.FontFamily, 25, FontStyle.Bold))
    {
        dataGridView1.Columns["Rating"].DefaultCellStyle.Font = font;
    }

    // Attach a handler to the CellFormatting event.
    dataGridView1.CellFormatting += new
        DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
}

Comentarios

El valor predeterminado DataGridViewCellStyle devuelto por esta propiedad tiene los siguientes valores de propiedad iniciales.

Propiedad DataGridViewCellStyle Valor predeterminado
BackColor Valor de propiedad SystemBrushes.Control.
ForeColor Valor de propiedad SystemBrushes.WindowText.
SelectionBackColor Valor de propiedad SystemBrushes.Highlight.
SelectionForeColor Valor de propiedad SystemBrushes.HighlightText.
Font Valor de propiedad Font.
Alignment DataGridViewContentAlignment.MiddleLeft
WrapMode DataGridViewTriState.True

Estos valores invalidan automáticamente los valores establecidos a través de la DefaultCellStyle propiedad . Para forzar que los encabezados de fila hereden los DefaultCellStyle valores, debe establecer los valores del RowHeadersDefaultCellStyle objeto en los valores predeterminados indicados para la DataGridViewCellStyle clase .

Para obtener más información sobre la herencia de estilos de celda, vea Estilos de celda en el control DataGridView de Windows Forms.

Se aplica a

Producto Versiones
.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, 10

Consulte también