Nasıl yapılır: Windows Forms DataGridView Denetimi İçin Varsayılan Hücre Stillerini Ayarlama

DataGridViewDenetimle, tüm denetim için varsayılan hücre stillerini ve belirli sütun ve satırları belirtebilirsiniz. Bu varsayılanlar, denetim düzeyinden sütun düzeyine, ardından satır düzeyine, sonra da hücre düzeyine göre filtreleyerek ayarlar. Belirli bir DataGridViewCellStyle özellik hücre düzeyinde ayarlanmamışsa, satır düzeyindeki varsayılan özellik ayarı kullanılır. Özellik satır düzeyinde de ayarlanmamışsa, varsayılan sütun ayarı kullanılır. Son olarak, özellik sütun düzeyinde de ayarlanmamışsa, varsayılan DataGridView ayar kullanılır. Bu ayarla, özellik ayarlarını birden çok düzeyde çoğaltmak zorunda kalmaktan kaçınabilirsiniz. Her düzeyde, yukarıdaki düzeyden farklı olan stilleri belirtmeniz yeterlidir. daha fazla bilgi için Windows Forms DataGridView denetimindeki hücre stilleribölümüne bakın.

Visual Studio içinde bu görev için kapsamlı destek vardır. ayrıca bkz. nasıl yapılır: tasarımcıyı kullanarak Windows Forms DataGridView denetimi için varsayılan hücre stillerini ve veri biçimlerini ayarlama.

Varsayılan hücre stillerini programlı olarak ayarlamak için

  1. DataGridViewCellStyleÖzelliği aracılığıyla alınan özelliklerini ayarlayın 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. DataGridViewCellStyleBirden çok satır ve sütun tarafından kullanılmak üzere yeni nesneler oluşturun ve başlatın.

    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. DefaultCellStyleBelirli satırların ve sütunların özelliğini ayarlayın.

    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
    

Örnek

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

Kod Derleniyor

Bu örnek şunları gerektirir:

Güçlü Programlama

Çok büyük veri kümeleriyle çalışırken en yüksek ölçeklenebilirlik elde etmek için DataGridViewCellStyle nesneleri tek tek öğelerin stil özelliklerini ayrı ayrı ayarlamak yerine, aynı stilleri kullanan birden çok satır, sütun veya hücrede paylaşabilirsiniz. Ayrıca, özelliğini kullanarak paylaşılan satırlar oluşturmanız ve bunlara erişmeniz gerekir DataGridViewRowCollection.SharedRow . daha fazla bilgi için bkz. Windows Forms DataGridView denetimini ölçeklendirme için en iyi uygulamalar.

Ayrıca bkz.