DataGridViewImageColumn.DefaultCellStyle DataGridViewImageColumn.DefaultCellStyle DataGridViewImageColumn.DefaultCellStyle DataGridViewImageColumn.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

屬性值

範例

下列程式碼範例示範如何使用這個屬性。The following code example demonstrates the use of this property.

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

備註

這個屬性DataGridViewCellStyle所傳回的預設值具有下列初始屬性值:The default DataGridViewCellStyle returned by this property has the following initial property values:

DataGridViewCellStyle 屬性DataGridViewCellStyle property 預設值Default value
Alignment DataGridViewContentAlignment.MiddleCenter
NullValue Icon如果初始ValuesAreIcons屬性值為true, 則為類型的標準錯誤圖形, 否則為類型Bitmap的標準錯誤圖形。A standard error graphic of type Icon if the initial ValuesAreIcons property value is true; otherwise, a standard error graphic of type Bitmap.

這些值會覆寫DefaultCellStyle DataGridView控制項的屬性中指定的值, 但可能被其他儲存格樣式屬性所覆寫。These values override the value specified in the DefaultCellStyle property of the DataGridView control, but may be overridden by other cell style properties. 如需詳細資訊, 請參閱Windows Forms DataGridView 控制項中的儲存格樣式For more information, see Cell Styles in the Windows Forms DataGridView Control.

若要防止null出現或DBNull.Value資料格值的標準錯誤圖形, 請將DataGridViewCellStyle.NullValue此儲存格樣式物件的屬性null設定為或您自己的錯誤圖形, 再將資料列加入至控制項。To prevent the standard error graphic from appearing for null or DBNull.Value cell values, set the DataGridViewCellStyle.NullValue property for this cell style object to null or your own error graphic before adding rows to the control. 不過, 這不會影響新記錄的資料列。This does not affect the row for new records, however. 若要防止錯誤圖形在控制項AllowUserToAddRows屬性值為true時, 出現在新記錄的資料列中, 您也必須在控制項null 的處理常式中明確地將儲存格值設定為或您自己的錯誤圖形RowsAdded事件, 或將資料CellTemplate行屬性設定為衍生null類型DataGridViewImageCell的實例, 其中包含會DefaultNewRowValue傳回或您自己的錯誤圖形的覆寫屬性。To prevent the error graphic from appearing in the row for new records when the control AllowUserToAddRows property value is true, you must also either explicitly set the cell value to null or your own error graphic in a handler for the control RowsAdded event or set the column CellTemplate property to an instance of a DataGridViewImageCell-derived type with an overridden DefaultNewRowValue property that returns null or your own error graphic.

如果DefaultCellStyle屬性NullValue所傳回之物件的屬性具有與類型Bitmap的標準錯誤圖形相等的值, 則ValuesAreIcons將屬性值變更為true自動設定NullValue為類型Icon的標準錯誤圖形。If the NullValue property of the object returned by the DefaultCellStyle property has a value equal to the standard error graphic of type Bitmap, changing the ValuesAreIcons property value to true automatically sets NullValue to the standard error graphic of type Icon. 如果NullValue的值等於類型Icon的標準錯誤圖形, 則ValuesAreIcons將屬性值變更為false會自動設定NullValue為類型Bitmap的標準錯誤圖形。If NullValue has a value equal to the standard error graphic of type Icon, changing the ValuesAreIcons property value to false automatically sets NullValue to the standard error graphic of type Bitmap.

適用於

另請參閱