DataGridViewCellStyle Class
Collapse the table of content
Expand the table of content

DataGridViewCellStyle Class


Represents the formatting and style information applied to individual cells within a DataGridView control.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)


public class DataGridViewCellStyle : ICloneable


Initializes a new instance of the DataGridViewCellStyle class using default property values.


Initializes a new instance of the DataGridViewCellStyle class using the property values of the specified DataGridViewCellStyle.


Gets or sets a value indicating the position of the cell content within a DataGridView cell.


Gets or sets the background color of a DataGridView cell.


Gets or sets the value saved to the data source when the user enters a null value into a cell.


Gets or sets the font applied to the textual content of a DataGridView cell.


Gets or sets the foreground color of a DataGridView cell.


Gets or sets the format string applied to the textual content of a DataGridView cell.


Gets or sets the object used to provide culture-specific formatting of DataGridView cell values.


Gets a value indicating whether the DataSourceNullValue property has been set.


Gets a value that indicates whether the FormatProvider property has been set.


Gets a value indicating whether the NullValue property has been set.


Gets or sets the DataGridView cell display value corresponding to a cell value of DBNull.Value or null.


Gets or sets the space between the edge of a DataGridViewCell and its content.


Gets or sets the background color used by a DataGridView cell when it is selected.


Gets or sets the foreground color used by a DataGridView cell when it is selected.


Gets or sets an object that contains additional data related to the DataGridViewCellStyle.


Gets or sets a value indicating whether textual content in a DataGridView cell is wrapped to subsequent lines or truncated when it is too long to fit on a single line.


Applies the specified DataGridViewCellStyle to the current DataGridViewCellStyle.


Creates an exact copy of this DataGridViewCellStyle.


Returns a value indicating whether this instance is equivalent to the specified object.(Overrides Object.Equals(Object).)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as a hash function for a particular type.(Overrides Object.GetHashCode().)


Gets the Type of the current instance.(Inherited from Object.)


Creates a shallow copy of the current Object.(Inherited from Object.)


Returns a string indicating the current property settings of the DataGridViewCellStyle.(Overrides Object.ToString().)


Creates an exact copy of this DataGridViewCellStyle.

The DataGridViewCellStyle class lets you share style information across multiple DataGridView cells, rows, columns, and row or column headers, avoiding the memory requirements of setting style properties on individual cells. For more information about classes that have a property of type DataGridViewCellStyle and how they relate to each other, see Cell Styles in the Windows Forms DataGridView Control.

The following code example demonstrates the effect of setting properties on multiple DataGridViewCellStyle objects. This example sets the background color of cells in the DataGridView by setting the BackColor property on the DefaultCellStyle property. The background color is overridden on alternating rows because the BackColor property is set on the AlternatingRowsDefaultCellStyle property. The example also determines the format of dates in the column named Last Prepared by setting the Format property on the column's DefaultCellStyle property.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
    public static void Main()
        Application.Run(new Form1());

    private DataGridView dataGridView1 = new DataGridView();

    protected override void OnLoad(EventArgs e)
        // Create the columns and load the data.

        // Configure the appearance and behavior of the DataGridView.

        // Initialize the form.
        this.Text = "DataGridView style demo";
        this.Size = new Size(600, 250);

    // 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 = 
        dataGridView1.AllowUserToResizeRows = false;
        dataGridView1.RowHeadersWidthSizeMode = 

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

    // Changes the foreground color of cells in the "Ratings" column 
    // depending on the number of stars. 
    private void dataGridView1_CellFormatting(object sender,
        DataGridViewCellFormattingEventArgs e)
        if (e.ColumnIndex == dataGridView1.Columns["Rating"].Index
            && e.Value != null)
            switch (e.Value.ToString().Length)
                case 1:
                    e.CellStyle.SelectionForeColor = Color.Red;
                    e.CellStyle.ForeColor = Color.Red;
                case 2:
                    e.CellStyle.SelectionForeColor = Color.Yellow;
                    e.CellStyle.ForeColor = Color.Yellow;
                case 3:
                    e.CellStyle.SelectionForeColor = Color.Green;
                    e.CellStyle.ForeColor = Color.Green;
                case 4:
                    e.CellStyle.SelectionForeColor = Color.Blue;
                    e.CellStyle.ForeColor = Color.Blue;

    // Creates the columns and loads the data.
    private void PopulateDataGridView()
        // Set the column header names.
        dataGridView1.ColumnCount = 5;
        dataGridView1.Columns[0].Name = "Recipe";
        dataGridView1.Columns[1].Name = "Category";
        dataGridView1.Columns[2].Name = "Main Ingredients";
        dataGridView1.Columns[3].Name = "Last Prepared";
        dataGridView1.Columns[4].Name = "Rating";

        // Populate the rows.
        object[] row1 = new object[]{"Meatloaf", "Main Dish", 
            "ground beef", new DateTime(2000, 3, 23), "*"};
        object[] row2 = new object[]{"Key Lime Pie", "Dessert", 
            "lime juice, evaporated milk", new DateTime(2002, 4, 12), "****"};
        object[] row3 = new object[]{"Orange-Salsa Pork Chops", "Main Dish", 
            "pork chops, salsa, orange juice", new DateTime(2000, 8, 9), "****"};
        object[] row4 = new object[]{"Black Bean and Rice Salad", "Salad", 
            "black beans, brown rice", new DateTime(1999, 5, 7), "****"};
        object[] row5 = new object[]{"Chocolate Cheesecake", "Dessert", 
            "cream cheese", new DateTime(2003, 3, 12), "***"};
        object[] row6 = new object[]{"Black Bean Dip", "Appetizer",
            "black beans, sour cream", new DateTime(2003, 12, 23), "***"};

        // Add the rows to the DataGridView.
        object[] rows = new object[] { row1, row2, row3, row4, row5, row6 };
        foreach (object[] rowArray in rows)

        // Adjust the row heights so that all content is visible.


.NET Framework
Available since 2.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft