Postupy: Nastavení výchozích stylů buňky pro ovládací prvek Windows Forms DataGridView
DataGridView Pomocí ovládacího prvku můžete zadat výchozí styly buněk pro celý ovládací prvek a pro konkrétní sloupce a řádky. Tyto výchozí hodnoty filtrují z úrovně ovládacího prvku na úroveň sloupce, potom na úroveň řádků a potom na úroveň buňky. Pokud konkrétní DataGridViewCellStyle vlastnost není nastavena na úrovni buňky, použije se výchozí nastavení vlastnosti na úrovni řádku. Pokud vlastnost také není nastavena na úrovni řádku, použije se výchozí nastavení sloupce. A konečně, pokud vlastnost není nastavena na úrovni sloupce, použije se výchozí DataGridView nastavení. Díky tomuto nastavení se můžete vyhnout duplikování nastavení vlastnosti na více úrovních. Na každé úrovni jednoduše zadejte styly, které se liší od úrovní nad ním. Další informace naleznete v části Styly buněk v ovládacím prvku model Windows Forms DataGridView.
V sadě Visual Studio existuje rozsáhlá podpora pro tuto úlohu. Viz Také viz Postupy: Nastavení výchozích stylů buněk a datových formátů pro model Windows Forms DataGridView ovládací prvek pomocí Návrháře.
Nastavení výchozích stylů buněk prostřednictvím kódu programu
Nastavte vlastnosti DataGridViewCellStyle načtené prostřednictvím DataGridView.DefaultCellStyle vlastnosti.
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)
Umožňuje vytvářet a inicializovat nové DataGridViewCellStyle objekty pro použití několika řádky a sloupci.
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
DefaultCellStyle
Nastavte vlastnost konkrétních řádků a sloupců.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
Příklad
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
Probíhá kompilace kódu
Tento příklad vyžaduje:
Ovládací DataGridView prvek s názvem
dataGridView1
.Odkazy na System, System.Drawinga System.Windows.Forms sestavení.
Robustní programování
Pokud chcete dosáhnout maximální škálovatelnosti při práci s velmi velkými datovými sadami, měli byste sdílet DataGridViewCellStyle objekty napříč více řádky, sloupci nebo buňkami, které používají stejné styly, a ne nastavit vlastnosti stylu pro jednotlivé prvky samostatně. Kromě toho byste měli vytvořit sdílené řádky a přistupovat k nim pomocí DataGridViewRowCollection.SharedRow vlastnosti. Další informace naleznete v tématu Osvědčené postupy pro škálování ovládacího prvku model Windows Forms DataGridView.
Viz také
- DataGridView
- DataGridViewCellStyle
- DataGridView.DefaultCellStyle
- DataGridViewBand.DefaultCellStyle
- Základní formátování a práce se styly v ovládacím prvku Windows Forms DataGridView
- Styly buňky v ovládacím prvku Windows Forms DataGridView
- Doporučené postupy pro změnu velikosti ovládacího prvku Windows Forms DataGridView
- Postupy: Nastavení střídavých stylů řádků pro ovládací prvek Windows Forms DataGridView
.NET Desktop feedback
Váš názor
https://aka.ms/ContentUserFeedback.
Připravujeme: V průběhu roku 2024 budeme postupně vyřazovat problémy z GitHub coby mechanismus zpětné vazby pro obsah a nahrazovat ho novým systémem zpětné vazby. Další informace naleznete v tématu:Odeslat a zobrazit názory pro