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

要套用至控制項顯示之文字的 Font。 預設值為 DefaultFont 屬性的值。

屬性

範例

下列程式碼範例說明如何使用這個屬性。 此範例是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

備註

屬性 Font 是環境屬性。 環境屬性是控制項屬性,如果未設定,則會從父控制項擷取。 例如, Button 預設會有與其父 Form 系相同的 BackColor 。 如需環境屬性的詳細資訊,請參閱 AmbientProperties 類別或類別概 Control 觀。

Font因為 是不可變的 (表示您無法調整其任何屬性) ,所以您只能將屬性指派 Font 給新的 Font 物件。 不過,您可以將新字型以現有的字型為基礎。

控制項 DataGridView 使用 屬性的值 Font 做為 、 ColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle 屬性所 DefaultCellStyle 傳回之物件的預設值 Font DataGridViewCellStyle 。 變更 Font 值會自動更新 DefaultCellStyleColumnHeadersDefaultCellStyleRowHeadersDefaultCellStyle 屬性,變更繼承值之任何儲存格的字型。 標頭儲存格預設會覆寫值,而且您可以覆寫特定資料列、欄和儲存格的值。 如需儲存格樣式繼承的詳細資訊,請參閱dataGridView 控制項中的資料格樣式Windows Forms

適用於

另請參閱