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

屬性

範例

下列程式碼範例示範如何使用這個屬性。

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 具有下列初始屬性值:

DataGridViewCellStyle 屬性 預設值
Alignment DataGridViewContentAlignment.MiddleCenter
NullValue 如果初始 ValuesAreIcons 屬性值為 true ,則為 類型的 Icon 標準錯誤圖形,否則為 類型的 Bitmap 標準錯誤圖形。

這些值會覆寫 控制項的 DataGridView 屬性中指定的 DefaultCellStyle 值,但可能會由其他儲存格樣式屬性覆寫。 如需詳細資訊,請參閱Windows Forms DataGridView 控制項中的儲存格樣式

若要防止標準錯誤圖形針對 或 DBNull.Value 儲存格值顯示 null ,請將此儲存格樣式物件的 屬性設定 DataGridViewCellStyle.NullValuenull 或您自己的錯誤圖形,再將資料列新增至 控制項。 不過,這不會影響新記錄的資料列。 若要防止當控制項 AllowUserToAddRows 屬性值為 true 時,新記錄的資料列中出現錯誤圖形,您也必須在控制項 RowsAdded 事件的處理常式中明確將儲存格值 null 設定為 或您自己的錯誤圖形,或將資料 CellTemplate 行屬性設定為衍生類型的實例 DataGridViewImageCell ,其中含有傳回 null 或您自己的錯誤圖形的覆 DefaultNewRowValue 寫屬性。

NullValue如果 屬性所 DefaultCellStyle 傳回之 物件的 屬性的值等於 類型的 Bitmap 標準錯誤圖形,則變更 ValuesAreIcons 屬性值以 true 自動設定 NullValue 為 類型的 Icon 標準錯誤圖形。 如果 NullValue 的值等於 類型的 Icon 標準錯誤圖形,請將 ValuesAreIcons 屬性值變更為 false 自動設定 NullValue 為 類型的 Bitmap 標準錯誤圖形。

適用於

另請參閱