DataGridView.Font 属性   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 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; }[<System.ComponentModel.Browsable(false)>]
member this.Font : System.Drawing.Font with get, setPublic Overrides Property Font As Font属性值
要应用于由控件显示的文本的 Font。 默认为 DefaultFont 属性的值。
- 属性
示例
下面的代码示例演示了此属性的使用。 此示例是“如何:创建 Unbound Windows 窗体 DataGridView 控件”中提供的大型示例的一部分。
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
注解
该 Font 属性是一个环境属性。 环境属性是一个控件属性,如果未设置,则从父控件检索该属性。 例如,默认情况下,a Button 将具有与其父级Form相同的BackColor值。 有关环境属性的详细信息,请参阅 AmbientProperties 类或 Control 类概述。
Font由于不可变 (意味着不能调整其任何属性) ,因此只能为属性分配Font一个新Font对象。 但是,可以将新字体基于现有字体。
该DataGridView控件使用属性的值Font作为由属性ColumnHeadersDefaultCellStyle和RowHeadersDefaultCellStyle属性返回DefaultCellStyle的对象属性DataGridViewCellStyle的Font默认值。 更改Font值会自动更新DefaultCellStyle和ColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle属性,从而更改继承该值的任何单元格的字体。 默认情况下,标题单元格会替代值,你可以替代特定行、列和单元格的值。 有关单元格样式继承的详细信息,请参阅 Windows 窗体 DataGridView 控件中的单元格样式。