Share via


How to: Set Default Cell Styles for the Windows Forms DataGridView Control

With the DataGridView control, you can specify default cell styles for the entire control and for specific columns and rows. These defaults filter down from the control level to the column level, then to the row level, then to the cell level. If a particular DataGridViewCellStyle property is not set at the cell level, the default property setting at the row level is used. If the property is also not set at the row level, the default column setting is used. Finally, if the property is also not set at the column level, the default DataGridView setting is used. With this setting, you can avoid having to duplicate the property settings at multiple levels. At each level, simply specify the styles that differ from the levels above it. For more information, see Cell Styles in the Windows Forms DataGridView Control.

There is extensive support for this task in Visual Studio. How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer
How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer
How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer
How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer

To set the default cell styles programmatically

  1. Set the properties of the DataGridViewCellStyle retrieved through the System.Windows.Forms.DataGridView.DefaultCellStyle property.

    Me.dataGridView1.DefaultCellStyle.BackColor = Color.Beige
    Me.dataGridView1.DefaultCellStyle.Font = New Font("Tahoma", 12)
    
    this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
    this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);
    
  2. Create and initialize new DataGridViewCellStyle objects for use by multiple rows and columns.

    Dim highlightCellStyle As New DataGridViewCellStyle
    highlightCellStyle.BackColor = Color.Red
    
    Dim currencyCellStyle As New DataGridViewCellStyle
    currencyCellStyle.Format = "C"
    currencyCellStyle.ForeColor = Color.Green
    
    DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle();
    highlightCellStyle.BackColor = Color.Red;
    
    DataGridViewCellStyle currencyCellStyle = new DataGridViewCellStyle();
    currencyCellStyle.Format = "C";
    currencyCellStyle.ForeColor = Color.Green;
    
  3. Set the DefaultCellStyle property of specific rows and columns.

    With Me.dataGridView1
        .Rows(3).DefaultCellStyle = highlightCellStyle
        .Rows(8).DefaultCellStyle = highlightCellStyle
        .Columns("UnitPrice").DefaultCellStyle = currencyCellStyle
        .Columns("TotalPrice").DefaultCellStyle = currencyCellStyle
    End With
    
    this.dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Rows[8].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle =
        currencyCellStyle;
    this.dataGridView1.Columns["TotalPrice"].DefaultCellStyle =
        currencyCellStyle;
    

Example

Private Sub SetDefaultCellStyles()

    Dim highlightCellStyle As New DataGridViewCellStyle
    highlightCellStyle.BackColor = Color.Red

    Dim currencyCellStyle As New DataGridViewCellStyle
    currencyCellStyle.Format = "C"
    currencyCellStyle.ForeColor = Color.Green

    With Me.dataGridView1
        .DefaultCellStyle.BackColor = Color.Beige
        .DefaultCellStyle.Font = New Font("Tahoma", 12)
        .Rows(3).DefaultCellStyle = highlightCellStyle
        .Rows(8).DefaultCellStyle = highlightCellStyle
        .Columns("UnitPrice").DefaultCellStyle = currencyCellStyle
        .Columns("TotalPrice").DefaultCellStyle = currencyCellStyle
    End With

End Sub
private void SetDefaultCellStyles()
{
    this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
    this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);

    DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle();
    highlightCellStyle.BackColor = Color.Red;

    DataGridViewCellStyle currencyCellStyle = new DataGridViewCellStyle();
    currencyCellStyle.Format = "C";
    currencyCellStyle.ForeColor = Color.Green;

    this.dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Rows[8].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle =
        currencyCellStyle;
    this.dataGridView1.Columns["TotalPrice"].DefaultCellStyle =
        currencyCellStyle;
}

Compiling the Code

This example requires:

Robust Programming

To achieve maximum scalability when you work with very large data sets, you should share DataGridViewCellStyle objects across multiple rows, columns, or cells that use the same styles, rather than set the style properties for individual elements separately. Additionally, you should create shared rows and access them by using the System.Windows.Forms.DataGridViewRowCollection.SharedRow(System.Int32) property. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

See Also

Tasks

How to: Set Alternating Row Styles for the Windows Forms DataGridView Control

Reference

DataGridView
DataGridViewCellStyle
System.Windows.Forms.DataGridView.DefaultCellStyle
System.Windows.Forms.DataGridViewBand.DefaultCellStyle

Concepts

Cell Styles in the Windows Forms DataGridView Control
Best Practices for Scaling the Windows Forms DataGridView Control

Other Resources

Basic Formatting and Styling in the Windows Forms DataGridView Control