DataGridView.Font Property

Definition

Gets or sets the font of the text displayed by the DataGridView.

C#
[System.ComponentModel.Browsable(false)]
public override System.Drawing.Font Font { get; set; }

Property Value

The Font to apply to the text displayed by the control. The default is the value of the DefaultFont property.

Attributes

Examples

The following code example illustrates the use of this property. This example is part of a larger example available in How to: Create an Unbound Windows Forms DataGridView Control.

C#
private void SetupDataGridView()
{
    this.Controls.Add(songsDataGridView);

    songsDataGridView.ColumnCount = 5;

    songsDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
    songsDataGridView.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
    songsDataGridView.ColumnHeadersDefaultCellStyle.Font =
        new Font(songsDataGridView.Font, FontStyle.Bold);

    songsDataGridView.Name = "songsDataGridView";
    songsDataGridView.Location = new Point(8, 8);
    songsDataGridView.Size = new Size(500, 250);
    songsDataGridView.AutoSizeRowsMode =
        DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
    songsDataGridView.ColumnHeadersBorderStyle =
        DataGridViewHeaderBorderStyle.Single;
    songsDataGridView.CellBorderStyle = DataGridViewCellBorderStyle.Single;
    songsDataGridView.GridColor = Color.Black;
    songsDataGridView.RowHeadersVisible = false;

    songsDataGridView.Columns[0].Name = "Release Date";
    songsDataGridView.Columns[1].Name = "Track";
    songsDataGridView.Columns[2].Name = "Title";
    songsDataGridView.Columns[3].Name = "Artist";
    songsDataGridView.Columns[4].Name = "Album";
    songsDataGridView.Columns[4].DefaultCellStyle.Font =
        new Font(songsDataGridView.DefaultCellStyle.Font, FontStyle.Italic);

    songsDataGridView.SelectionMode =
        DataGridViewSelectionMode.FullRowSelect;
    songsDataGridView.MultiSelect = false;
    songsDataGridView.Dock = DockStyle.Fill;

    songsDataGridView.CellFormatting += new
        DataGridViewCellFormattingEventHandler(
        songsDataGridView_CellFormatting);
}

Remarks

The Font property is an ambient property. An ambient property is a control property that, if not set, is retrieved from the parent control. For example, a Button will have the same BackColor as its parent Form by default. For more information about ambient properties, see the AmbientProperties class or the Control class overview.

Because the Font is immutable (meaning that you cannot adjust any of its properties), you can only assign the Font property a new Font object. However, you can base the new font on the existing font.

The DataGridView control uses the value of the Font property as the default value of the Font properties of DataGridViewCellStyle objects returned by the DefaultCellStyle, ColumnHeadersDefaultCellStyle, and RowHeadersDefaultCellStyle properties. Changing the Font value automatically updates the DefaultCellStyle, ColumnHeadersDefaultCellStyle, and RowHeadersDefaultCellStyle properties, changing the font for any cell that inherits the value. Header cells override the value by default, and you can override the value for specific rows, columns, and cells. For more information about cell style inheritance, see Cell Styles in the Windows Forms DataGridView Control.

Applies to

Product Versions
.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

See also