共用方式為


DataGridView.Font 屬性

定義

取得或設定由 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, set
Public Overrides Property Font As Font

屬性值

Font將 套用到控制項顯示的文字上。 預設是該房產的 DefaultFont 價值。

屬性

範例

以下程式碼範例說明此特性的使用。 此範例是《 如何建立未綁定的 Windows Forms 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。 欲了解更多環境特性資訊,請參閱職業ControlAmbientProperties職業概述。

因為 是 Font 不可變的(也就是說你無法調整其任何屬性),你只能把這個 Font 屬性指派一個新的 Font 物件。 不過,你可以以現有字型為基礎來設計新字型。

控制項DataGridView使用屬性的Font值作為物件屬性的預設值,該FontDataGridViewCellStyle屬性由 DefaultCellStyleColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle 屬性回傳。 更改值會 Font 自動更新 DefaultCellStyleColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle 屬性,並改變繼承該值的儲存格的字型。 標頭儲存格預設會覆蓋該值,且你可以針對特定列、列和儲存格覆蓋該值。 欲了解更多關於儲存格樣式繼承的資訊,請參閱 Windows 表單 DataGridView 控制項中的儲存格樣式。

適用於

另請參閱