如何:在 Windows Form DataGridView 控制項中使用資料列範本自訂資料列
控制項 DataGridView 會使用資料列範本作為它透過資料系結新增至控制項之所有資料列的基礎,或當您呼叫 DataGridViewRowCollection.Add 方法時,而不需指定要使用的現有資料列。
資料列範本可讓您更充分掌控屬性所提供的資料列 RowsDefaultCellStyle 外觀和行為。 透過資料列範本,您可以設定任何 DataGridViewRow 屬性,包括 DefaultCellStyle 。
在某些情況下,您必須使用資料列範本來達到特定效果。 例如,資料列高度資訊無法儲存在 中 DataGridViewCellStyle ,因此您必須使用資料列範本來變更所有資料列所使用的預設高度。 當您建立衍生自 DataGridViewRow 的類別,而且想要在將新資料列新增至 控制項時使用自訂類型時,資料列範本也很有用。
注意
只有在加入資料列時,才會使用資料列範本。 您無法藉由變更資料列範本來變更現有的資料列。
若要使用資料列範本
在從 DataGridView.RowTemplate 屬性擷取的物件上設定屬性。
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;
With Me.dataGridView1.RowTemplate .DefaultCellStyle.BackColor = Color.Bisque .Height = 35 .MinimumHeight = 20 End With
編譯程式碼
這個範例需要:
名為
dataGridView1
的 DataGridView 控制項。System、System.Drawing 和 System.Windows.Forms 組件的參考。
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應