如何:設定 Windows Form DataGridView 控制項的預設儲存格樣式

您可以使用 DataGridView 控制項為整個控制項,以及為特定資料行和資料列,指定預設儲存格樣式。 這些預設值會從控制項層級往下篩選至資料行層級,接著至資料列層級,然後至儲存格層級。 如果在儲存格層級未設定特定 DataGridViewCellStyle 屬性,則會使用資料列層級的預設屬性設定。 如果在資料列層級也未設定這個屬性,則會使用預設資料行設定。 最後,如果在資料行層級還是未設定這個屬性,則會使用預設 DataGridView 設定。 您可以利用這項設定,避免必須在多個層級重複設定屬性。 您只需要在每個層級指定不同於上一層級的樣式。 如需詳細資訊,請參閱 Windows Forms DataGridView 控制項 中的儲存格樣式。

在 Visual Studio 中對於本工作有更詳盡的支援。 另請參閱 如何:使用設計 工具設定 Windows Forms DataGridView 控制項的預設儲存格樣式和資料格式。

以程式設計方式設定預設儲存格樣式

  1. 設定透過 DataGridView.DefaultCellStyle 屬性擷取之 DataGridViewCellStyle 的屬性。

    this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
    this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);
    
    Me.dataGridView1.DefaultCellStyle.BackColor = Color.Beige
    Me.dataGridView1.DefaultCellStyle.Font = New Font("Tahoma", 12)
    
  2. 建立並初始化新的 DataGridViewCellStyle 物件,以供多個資料列和資料行使用。

    DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle();
    highlightCellStyle.BackColor = Color.Red;
    
    DataGridViewCellStyle currencyCellStyle = new DataGridViewCellStyle();
    currencyCellStyle.Format = "C";
    currencyCellStyle.ForeColor = Color.Green;
    
    Dim highlightCellStyle As New DataGridViewCellStyle
    highlightCellStyle.BackColor = Color.Red
    
    Dim currencyCellStyle As New DataGridViewCellStyle
    currencyCellStyle.Format = "C"
    currencyCellStyle.ForeColor = Color.Green
    
  3. 設定特定資料列和資料行的 DefaultCellStyle 屬性。

    this.dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Rows[8].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle =
        currencyCellStyle;
    this.dataGridView1.Columns["TotalPrice"].DefaultCellStyle =
        currencyCellStyle;
    
    With Me.dataGridView1
        .Rows(3).DefaultCellStyle = highlightCellStyle
        .Rows(8).DefaultCellStyle = highlightCellStyle
        .Columns("UnitPrice").DefaultCellStyle = currencyCellStyle
        .Columns("TotalPrice").DefaultCellStyle = currencyCellStyle
    End With
    

範例

private void SetDefaultCellStyles()
{
    this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
    this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);

    DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle();
    highlightCellStyle.BackColor = Color.Red;

    DataGridViewCellStyle currencyCellStyle = new DataGridViewCellStyle();
    currencyCellStyle.Format = "C";
    currencyCellStyle.ForeColor = Color.Green;

    this.dataGridView1.Rows[3].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Rows[8].DefaultCellStyle = highlightCellStyle;
    this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle =
        currencyCellStyle;
    this.dataGridView1.Columns["TotalPrice"].DefaultCellStyle =
        currencyCellStyle;
}
Private Sub SetDefaultCellStyles()

    Dim highlightCellStyle As New DataGridViewCellStyle
    highlightCellStyle.BackColor = Color.Red

    Dim currencyCellStyle As New DataGridViewCellStyle
    currencyCellStyle.Format = "C"
    currencyCellStyle.ForeColor = Color.Green

    With Me.dataGridView1
        .DefaultCellStyle.BackColor = Color.Beige
        .DefaultCellStyle.Font = New Font("Tahoma", 12)
        .Rows(3).DefaultCellStyle = highlightCellStyle
        .Rows(8).DefaultCellStyle = highlightCellStyle
        .Columns("UnitPrice").DefaultCellStyle = currencyCellStyle
        .Columns("TotalPrice").DefaultCellStyle = currencyCellStyle
    End With

End Sub

編譯程式碼

這個範例需要:

穩固程式設計

若要在您使用非常大量的資料集時達到最大延展性,您應該共用使用相同樣式的多個資料列、資料行或儲存格之間的 DataGridViewCellStyle 物件,而不是分別設定每個項目的樣式屬性。 此外,您應該建立共用資料列,並使用 DataGridViewRowCollection.SharedRow 屬性來加以存取。 如需詳細資訊,請參閱 調整 Windows Forms DataGridView 控制項 的最佳做法。

另請參閱