DataGridViewColumn.DefaultCellStyle DataGridViewColumn.DefaultCellStyle DataGridViewColumn.DefaultCellStyle DataGridViewColumn.DefaultCellStyle Property

定義

取得或設定資料行的預設儲存格樣式。Gets or sets the column's default cell style.

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; }
member this.DefaultCellStyle : System.Windows.Forms.DataGridViewCellStyle with get, set
Public Overrides Property DefaultCellStyle As DataGridViewCellStyle

屬性值

DataGridViewCellStyle,表示資料行中儲存格的預設樣式。A DataGridViewCellStyle that represents the default style of the cells in the column.

範例

下列程式碼範例會使用DefaultCellStyle屬性來設定各種資料行的內容對齊。The following code example uses the DefaultCellStyle property to set the content alignment of various columns.

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

備註

控制項會使用資料格InheritedStyle屬性所指示的樣式來顯示其資料格,而這會繼承類型DataGridViewCellStyle之其他屬性的樣式。 DataGridViewThe DataGridView control displays its cells using the styles indicated by the cell InheritedStyle property, which inherits styles from other properties of type DataGridViewCellStyle. 對於此資料行中的儲存格,透過DefaultCellStyle屬性指定的樣式會覆寫DataGridView.DefaultCellStyle透過屬性指定的樣式,但會由DataGridView.RowsDefaultCellStyleDataGridView.AlternatingRowsDefaultCellStyleDataGridViewRow.DefaultCellStyle和所指定的樣式所覆寫。DataGridViewCell.Style屬性。For cells in this column, the styles specified through the DefaultCellStyle property override the styles specified through the DataGridView.DefaultCellStyle property, but are overridden by the styles specified through the DataGridView.RowsDefaultCellStyle, DataGridView.AlternatingRowsDefaultCellStyle, DataGridViewRow.DefaultCellStyle, and DataGridViewCell.Style properties.

如需詳細資訊,請參閱Windows Forms DataGridView 控制項中的儲存格樣式For more information, see Cell Styles in the Windows Forms DataGridView Control.

適用於

另請參閱