Share via


Como: conjunto estilos de célula padrão para o Windows Forms DataGridView controle

Com o DataGridView controle, você pode especificar estilos de célula padrão em todo o controle e em colunas específicas e linhas. Filtrar esses padrões para baixo do nível de controle para o nível de coluna, depois para o nível de linha e, em seguida, no nível da célula.Se um determinado DataGridViewCellStyle propriedade não está conjunto no nível da célula, a propriedade conjunto seletiva padrão no nível da linha é usada. Se a propriedade também não for conjunto no nível de linha, coluna conjunto seletiva a padrão é usada.Finalmente, se a propriedade não também estiver definida no nível de coluna, o padrão de DataGridView configuração é usada. Com essa configuração, você pode evitar a necessidade de duplicar as configurações de propriedade em vários níveis.Em cada nível simplesmente especificar os estilos diferentes dos níveis acima dele.Para obter mais informações, consulte Estilos de célula no Windows Forms DataGridView controle.

Há suporte extensivo para esta tarefa no Visual Studio.

Para conjunto o padrão de estilos de célula programaticamente

  1. conjunto as propriedades do DataGridViewCellStyle recuperados através de DataGridView.DefaultCellStyle propriedade.

    Me.dataGridView1.DefaultCellStyle.BackColor = Color.Beige
    Me.dataGridView1.DefaultCellStyle.Font = New Font("Tahoma", 12)
    
    this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
    this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 12);
    
  2. Criar e inicializar novos DataGridViewCellStyle objetos para uso por várias linhas e colunas.

    Dim highlightCellStyle As New DataGridViewCellStyle
    highlightCellStyle.BackColor = Color.Red
    
    Dim currencyCellStyle As New DataGridViewCellStyle
    currencyCellStyle.Format = "C"
    currencyCellStyle.ForeColor = Color.Green
    
    DataGridViewCellStyle highlightCellStyle = new DataGridViewCellStyle();
    highlightCellStyle.BackColor = Color.Red;
    
    DataGridViewCellStyle currencyCellStyle = new DataGridViewCellStyle();
    currencyCellStyle.Format = "C";
    currencyCellStyle.ForeColor = Color.Green;
    
  3. conjunto o DefaultCellStyle propriedade de específico de linhas e colunas.

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

Exemplo

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
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;
}

Compilando o código

Este exemplo requer:

Programação robusta

Para obter escalabilidade máxima quando você trabalha com conjuntos de dados muito grandes, você deve compartilhar DataGridViewCellStyle objetos em várias linhas, colunas ou células que usam os mesmos estilos, em vez de conjunto as propriedades de estilo para elementos individuais separadamente. Além disso, você deve criar linhas compartilhadas e acessá-las, usando o DataGridViewRowCollection.SharedRow propriedade. Para obter mais informações, consulte Práticas recomendadas para escala o controle DataGridView do Windows Forms.

Consulte também

Tarefas

Como: conjunto alternadas estilos de linha para o controle DataGridView do Windows Forms

Conceitos

Estilos de célula no Windows Forms DataGridView controle

Práticas recomendadas para escala o controle DataGridView do Windows Forms

Referência

DataGridView

DataGridViewCellStyle

DataGridView.DefaultCellStyle

DataGridViewBand.DefaultCellStyle

Outros recursos

A formatação básica e o estilo in Windows Forms DataGridView controle