Share via


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

Denetimle, denetimin DataGridView tamamı ve belirli sütunlar ve satırlar için varsayılan hücre stillerini belirtebilirsiniz. Bu varsayılanlar, denetim düzeyinden sütun düzeyine, sonra satır düzeyine ve ardından hücre düzeyine kadar filtreyi aşağı doğru filtreler. Belirli DataGridViewCellStyle bir özellik hücre düzeyinde ayarlanmadıysa, satır düzeyindeki varsayılan özellik ayarı kullanılır. Özellik de satır düzeyinde ayarlanmadıysa, varsayılan sütun ayarı kullanılır. Son olarak, özellik sütun düzeyinde de ayarlanmadıysa varsayılan DataGridView ayar kullanılır. Bu ayarla, özellik ayarlarını birden çok düzeyde yinelemek zorunda kalmaktan kaçınabilirsiniz. Her düzeyde, yukarıdaki düzeylerden farklı stilleri belirtmeniz yeterlidir. Daha fazla bilgi için bkz . Windows Forms DataGridView Denetimindeki Hücre Stilleri.

Visual Studio'da bu görev için kapsamlı destek sağlanır. Ayrıca bkz. Nasıl yapılır: Tasarım Aracı Kullanarak Windows Forms DataGridView Denetimi için Varsayılan Hücre Stillerini ve Veri Biçimlerini Ayarlama.

Varsayılan hücre stillerini program aracılığıyla ayarlamak için

  1. özelliği aracılığıyla alınan öğesinin DataGridView.DefaultCellStyle özelliklerini DataGridViewCellStyle ayarlayın.

    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. Birden çok satır ve sütun tarafından kullanılmak üzere yeni DataGridViewCellStyle 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. DefaultCellStyle Belirli satır 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 maksimum ölçeklenebilirlik elde etmek için, nesneleri tek tek öğelerin stil özelliklerini ayrı ayrı ayarlamak yerine aynı stilleri kullanan birden çok satır, sütun veya hücrede paylaşmanız DataGridViewCellStyle gerekir. Ayrıca, paylaşılan satırlar oluşturmanız ve özelliğini kullanarak DataGridViewRowCollection.SharedRow bunlara erişmeniz gerekir. Daha fazla bilgi için bkz . Windows Forms DataGridView Denetimini Ölçeklendirmeye yönelik En İyi Yöntemler.

Ayrıca bkz.