Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

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
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 DataGridView.DefaultCellStyle property.

    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.

    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.

    this.dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Rows[8].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle =
        currencyCellStyle;
    this.dataGridView1.Columns["TotalPrice"].DefaultCellStyle =
        currencyCellStyle;
    
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;
}

This example requires:

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 DataGridViewRowCollection.SharedRow property. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.