DataGridView.Font 屬性


取得或設定 DataGridView 顯示的文字字型。Gets or sets the font of the text displayed by the DataGridView.

 virtual property System::Drawing::Font ^ Font { System::Drawing::Font ^ get(); void set(System::Drawing::Font ^ value); };
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. 這個範例是 如何:建立未系結的 Windows Forms DataGridView 控制項中可用之較大範例的一部分。This example is part of a larger example available in How to: Create an Unbound Windows Forms DataGridView Control.

private void SetupDataGridView()

    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 =
    songsDataGridView.ColumnHeadersBorderStyle =
    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 =
    songsDataGridView.MultiSelect = false;
    songsDataGridView.Dock = DockStyle.Fill;

    songsDataGridView.CellFormatting += new
Private Sub SetupDataGridView()


    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 = _
        .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屬性是環境屬性。The Font property is an ambient property. 環境屬性是一個控制項屬性,如果未設定此屬性,就會從父控制項取出。An ambient property is a control property that, if not set, is retrieved from the parent control. 例如, Button 預設會有 BackColor 與其父系相同的 FormFor example, a Button will have the same BackColor as its parent Form by default. 如需環境屬性的詳細資訊,請參閱 AmbientProperties 類別或 Control 類別總覽。For more information about ambient properties, see the AmbientProperties class or the Control class overview.

因為 Font 是不可變的 (這表示您無法調整) 的任何屬性,所以只能將屬性指派給 Font 新的 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.

DataGridView控制項使用屬性的值 Font 做為 Font DataGridViewCellStyle DefaultCellStyleColumnHeadersDefaultCellStyle 和屬性所傳回之物件屬性的預設值 RowHeadersDefaultCellStyleThe 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. 變更 Font 值會自動更新 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 Forms DataGridView 控制項中的儲存格樣式For more information about cell style inheritance, see Cell Styles in the Windows Forms DataGridView Control.