Porady: ustawianie domyślnych stylów komórki dla formantu DataGridView formularzy systemu Windows

Za pomocą kontrolki DataGridView można określić domyślne style komórek dla całej kontrolki oraz dla określonych kolumn i wierszy. Te wartości domyślne filtruje z poziomu kontrolki do poziomu kolumny, a następnie do poziomu wiersza, a następnie do poziomu komórki. Jeśli określona DataGridViewCellStyle właściwość nie jest ustawiona na poziomie komórki, zostanie użyte domyślne ustawienie właściwości na poziomie wiersza. Jeśli właściwość nie jest również ustawiona na poziomie wiersza, zostanie użyte domyślne ustawienie kolumny. Na koniec, jeśli właściwość nie jest również ustawiona na poziomie kolumny, jest używane ustawienie domyślne DataGridView . Dzięki temu ustawieniu można uniknąć konieczności duplikowania ustawień właściwości na wielu poziomach. Na każdym poziomie po prostu określ style, które różnią się od powyższych poziomów. Aby uzyskać więcej informacji, zobacz Style komórek w kontrolce DataGridView formularzy systemu Windows.

W programie Visual Studio dostępna jest obszerna obsługa tego zadania. Zobacz również Instrukcje: ustawianie domyślnych stylów komórek i formatów danych dla kontrolki DataGridView formularzy systemu Windows przy użyciu Projektant.

Aby ustawić domyślne style komórek programowo

  1. Ustaw właściwości DataGridViewCellStyle pobrane za DataGridView.DefaultCellStyle pośrednictwem właściwości .

    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. Utwórz i zainicjuj nowe DataGridViewCellStyle obiekty do użycia przez wiele wierszy i kolumn.

    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 Ustaw właściwość określonych wierszy i kolumn.

    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
    

Przykład

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

Kompilowanie kodu

Ten przykład wymaga:

Niezawodne programowanie

Aby osiągnąć maksymalną skalowalność podczas pracy z bardzo dużymi zestawami danych, należy współużytkować DataGridViewCellStyle obiekty w wielu wierszach, kolumnach lub komórkach, które używają tych samych stylów, zamiast ustawiać właściwości stylu dla poszczególnych elementów oddzielnie. Ponadto należy utworzyć udostępnione wiersze i uzyskać do nich dostęp przy użyciu DataGridViewRowCollection.SharedRow właściwości . Aby uzyskać więcej informacji, zobacz Best Practices for Scaling the Windows Forms DataGridView Control (Najlepsze rozwiązania dotyczące skalowania kontrolki DataGridView formularzy systemu Windows).

Zobacz też