Export (0) Print
Expand All

How to: Set Font and Color Styles in the Windows Forms DataGridView Control

You can specify the visual appearance of cells within a DataGridView control by setting properties of the DataGridViewCellStyle class. You can retrieve instances of this class from various properties of the DataGridView class and its companion classes, or you can instantiate DataGridViewCellStyle objects for assignment to these properties.

The following procedures demonstrate basic customization of cell appearance using the DefaultCellStyle property. Every cell in the control inherits the styles specified through this property unless they are overridden at the column, row, or cell level. For an example of style inheritance, see How to: Set Default Cell Styles for the Windows Forms DataGridView Control. For information about additional uses of the DataGridViewCellStyle class, see the topics listed in the See Also section.

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

To specify the font used by DataGridView cells

To specify the foreground and background colors of DataGridView cells

To specify the foreground and background colors of selected DataGridView cells


private void SetFontAndColors()
{
    this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 15);
    this.dataGridView1.DefaultCellStyle.ForeColor = Color.Blue;
    this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
    this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;
    this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
}


This example requires:

For maximum scalability, you should share DataGridViewCellStyle objects across multiple rows, columns, or cells that use the same styles, rather than setting the style properties for each element separately. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

Community Additions

ADD
Show:
© 2014 Microsoft