共用方式為


DataGridViewColumn.DefaultCellStyle 屬性

定義

取得或設定資料行的預設儲存格樣式。

public:
 virtual property System::Windows::Forms::DataGridViewCellStyle ^ DefaultCellStyle { System::Windows::Forms::DataGridViewCellStyle ^ get(); void set(System::Windows::Forms::DataGridViewCellStyle ^ value); };
[System.ComponentModel.Browsable(true)]
public override System.Windows.Forms.DataGridViewCellStyle DefaultCellStyle { get; set; }
[<System.ComponentModel.Browsable(true)>]
member this.DefaultCellStyle : System.Windows.Forms.DataGridViewCellStyle with get, set
Public Overrides Property DefaultCellStyle As DataGridViewCellStyle

屬性值

DataGridViewCellStyle

DataGridViewCellStyle,表示資料行中儲存格的預設樣式。

屬性

範例

下列程式碼範例會使用 DefaultCellStyle 屬性來設定各種資料行的內容對齊方式。

private void ChangeColumnAlignment()
{
    songsDataGridView.Columns["Title"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter;
    songsDataGridView.Columns["Title"].Name = DataGridViewContentAlignment.BottomCenter.ToString();

    songsDataGridView.Columns["Artist"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft;
    songsDataGridView.Columns["Artist"].Name = DataGridViewContentAlignment.BottomLeft.ToString();

    songsDataGridView.Columns["Album"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
    songsDataGridView.Columns["Album"].Name = DataGridViewContentAlignment.BottomRight.ToString();

    songsDataGridView.Columns["Release Date"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
    songsDataGridView.Columns["Release Date"].Name = DataGridViewContentAlignment.MiddleCenter.ToString();

    songsDataGridView.Columns["Track"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
    songsDataGridView.Columns["Track"].Name = DataGridViewContentAlignment.MiddleLeft.ToString();
}
Private Sub ChangeColumnAlignment()
    songsDataGridView.Columns("Title").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
    songsDataGridView.Columns("Title").Name = DataGridViewContentAlignment.BottomCenter.ToString()

    songsDataGridView.Columns("Artist").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
    songsDataGridView.Columns("Artist").Name = DataGridViewContentAlignment.BottomLeft.ToString()

    songsDataGridView.Columns("Album").DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight
    songsDataGridView.Columns("Album").Name = DataGridViewContentAlignment.BottomRight.ToString()

    songsDataGridView.Columns("Release Date").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    songsDataGridView.Columns("Release Date").Name = DataGridViewContentAlignment.MiddleCenter.ToString()

    songsDataGridView.Columns("Track").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
    songsDataGridView.Columns("Track").Name = DataGridViewContentAlignment.MiddleLeft.ToString()
End Sub

備註

DataGridView控制項會使用儲存格屬性所指示的樣式來顯示其儲存格 InheritedStyle ,這會繼承類型 DataGridViewCellStyle 之其他屬性的樣式。 對於此資料行中的儲存格,透過 DefaultCellStyle 屬性指定的樣式會覆寫透過 DataGridView.DefaultCellStyle 屬性指定的樣式,但由透過 、 DataGridView.AlternatingRowsDefaultCellStyle DataGridViewRow.DefaultCellStyleDataGridViewCell.Style 屬性指定的 DataGridView.RowsDefaultCellStyle 樣式覆寫。

如需詳細資訊,請參閱DataGridView 控制項中Windows Forms儲存格樣式

適用於

另請參閱