Udostępnij za pośrednictwem


Jak ustawić domyślne style komórek dla kontrolki DataGridView formularzy Windows Forms

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 są filtrowane z poziomu kontrolki na poziom kolumny, następnie na poziom wiersza, a potem na poziom komórki. Jeśli określona właściwość DataGridViewCellStyle 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, zostanie użyte domyślne ustawienie 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 dla formularzy Windows Forms.

W programie Visual Studio dostępna jest obszerna obsługa tego zadania. Zobacz również Jak ustawić domyślne style komórek i formaty danych dla kontrolki Windows Forms DataGridView przy użyciu projektanta.

Aby ustawić domyślne style komórek programowo

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

    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 nowe obiekty DataGridViewCellStyle i zainicjuj je 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. Ustaw właściwość DefaultCellStyle 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 udostępnić DataGridViewCellStyle obiektów 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 właściwości DataGridViewRowCollection.SharedRow. Aby uzyskać więcej informacji, zobacz Najlepsze praktyki skalowania kontrolki DataGridView w Windows Forms.

Zobacz także