DataGridView.Font 属性

定义

获取或设置 DataGridView 显示的文本的字体。Gets or sets the font of the text displayed by the DataGridView.

public:
 virtual property System::Drawing::Font ^ Font { System::Drawing::Font ^ get(); void set(System::Drawing::Font ^ value); };
[System.ComponentModel.Browsable(false)]
public override System.Drawing.Font Font { get; set; }
member this.Font : System.Drawing.Font with get, set
Public Overrides Property Font As Font

属性值

要应用于由控件显示的文本的 FontThe Font to apply to the text displayed by the control. 默认为 DefaultFont 属性的值。The default is the value of the DefaultFont property.

属性

示例

下面的代码示例演示如何使用此属性。The following code example illustrates the use of this property. 此示例摘自 [How 中提供的更大示例的一部分:创建未绑定的 Windows 窗体 DataGridView 控件 @ no__t-0。This example is part of a larger example available in How to: Create an Unbound Windows Forms DataGridView Control.

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);
}
Private Sub SetupDataGridView()

    Me.Controls.Add(songsDataGridView)

    songsDataGridView.ColumnCount = 5
    With songsDataGridView.ColumnHeadersDefaultCellStyle
        .BackColor = Color.Navy
        .ForeColor = Color.White
        .Font = New Font(songsDataGridView.Font, FontStyle.Bold)
    End With

    With songsDataGridView
        .Name = "songsDataGridView"
        .Location = New Point(8, 8)
        .Size = New Size(500, 250)
        .AutoSizeRowsMode = _
            DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
        .ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
        .CellBorderStyle = DataGridViewCellBorderStyle.Single
        .GridColor = Color.Black
        .RowHeadersVisible = False

        .Columns(0).Name = "Release Date"
        .Columns(1).Name = "Track"
        .Columns(2).Name = "Title"
        .Columns(3).Name = "Artist"
        .Columns(4).Name = "Album"
        .Columns(4).DefaultCellStyle.Font = _
            New Font(Me.songsDataGridView.DefaultCellStyle.Font, FontStyle.Italic)

        .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        .MultiSelect = False
        .Dock = DockStyle.Fill
    End With

End Sub

注解

@No__t 的属性为环境属性。The Font property is an ambient property. 环境属性是一个控件属性,如果未设置,则将从父控件中检索该属性。An ambient property is a control property that, if not set, is retrieved from the parent control. 例如,默认情况下,@no__t 0 将具有与父 @no__t 相同的 @no__t。For example, a Button will have the same BackColor as its parent Form by default. 有关环境属性的详细信息,请参阅 AmbientProperties 类或 @no__t 类概述。For more information about ambient properties, see the AmbientProperties class or the Control class overview.

由于 @no__t 0 是不可变的(这意味着你无法调整其任何属性),因此你只能将 @no__t 的属性分配给新的 Font 对象。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.

@No__t-0 控件使用 Font 属性的值作为 @no__t、@no__t 和 @no__t 属性返回的 DataGridViewCellStyle 对象的 @no__t 属性的默认值的值。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. 更改 @no__t 值0会自动更新 DefaultCellStyleColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle 属性,从而更改继承该值的任何单元的字体。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. 有关单元样式继承的详细信息,请参阅Windows 窗体 DataGridView 控件中的单元格样式For more information about cell style inheritance, see Cell Styles in the Windows Forms DataGridView Control.

适用于

另请参阅