共用方式為


HOW TO:在 Windows Form DataGridView 控制項中使用資料列範本自訂資料列

DataGridView 控制項使用資料列範本做為加入至控制項的所有資料列的基準,控制項是經由資料繫結 (Data Binding),或當您呼叫 DataGridViewRowCollection.Add 方法而不指定要使用的現有資料列時,加入所有的資料列。

在資料列的外觀和行為上,資料列範本提供給您的控制,會 RowsDefaultCellStyle 屬性所提供的更好。 您可使用資料列範本設定任何 DataGridViewRow 屬性,包括 DefaultCellStyle

在某些情況下,您必須使用資料列範本以達成特定的效果。 例如,資料列高度資訊無法儲存於 DataGridViewCellStyle,所以您必須使用資料列範本變更所有資料列使用的預設高度。 當您建立衍生自 DataGridViewRow 的類別,且想要在加入新資料列至控制項中時使用自訂型別時,資料列範本也會很有用。

注意事項注意事項

只有在加入資料列時才會使用資料列範本。 您無法藉由變更資料列範本來變更現有的資料列。

若要使用資料列範本

  • 在擷取自 DataGridView.RowTemplate 屬性的物件上設定屬性。

    With Me.dataGridView1.RowTemplate
        .DefaultCellStyle.BackColor = Color.Bisque
        .Height = 35
        .MinimumHeight = 20
    End With
    
    DataGridViewRow row = this.dataGridView1.RowTemplate;
    row.DefaultCellStyle.BackColor = Color.Bisque;
    row.Height = 35;
    row.MinimumHeight = 20;
    
    DataGridViewRow^ row = this->dataGridView1->RowTemplate;
    row->DefaultCellStyle->BackColor = Color::Bisque;
    row->Height = 35;
    row->MinimumHeight = 20;
    
    

編譯程式碼

這項範例需要:

請參閱

參考

DataGridView

DataGridViewCellStyle

DataGridViewRow

DataGridView.RowTemplate

概念

Windows Form DataGridView 控制項中的儲存格樣式

其他資源

Windows Form DataGridView 控制項中的基本格式化和樣式設定