使用 DataGridView 控件可以为整个控件以及为特定列和行指定默认单元格样式。 这些默认值从控件级向下筛选至列级,再到行级,然后到单元格级。 如果在单元格级未设置某一特定 DataGridViewCellStyle 属性,则使用行级的默认属性设置。 如果在行级也未设置该属性,则使用默认列设置。 最后,如果在列级也未设置该属性,则使用默认的 DataGridView 设置。 使用此设置可避免必须在多个级别重复属性设置。 在每一个级别,只需简单地指定不同于上面各级的样式。 有关更多信息,请参见 Windows 窗体 DataGridView 控件中的单元格样式。
Visual Studio 中对此任务提供了广泛的支持。 有关更多信息,请参见 如何:使用设计器设置 Windows 窗体 DataGridView 控件的默认单元格样式和数据格式 和 如何:使用设计器设置 Windows 窗体 DataGridView 控件的默认单元格样式和数据格式 和 如何:使用设计器设置 Windows 窗体 DataGridView 控件的默认单元格样式和数据格式 和 如何:使用设计器设置 Windows 窗体 DataGridView 控件的默认单元格样式和数据格式.
以编程方式设置默认单元格样式
- 设置通过 DataGridView.DefaultCellStyle 属性检索的 DataGridViewCellStyle 的属性。 - 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);
- 创建和初始化新的 DataGridViewCellStyle 对象以供多个行和列使用。 - 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;
- 设置特定行和列的 DefaultCellStyle 属性。 - 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;
示例
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;
}
编译代码
此示例需要:
- 名为 dataGridView1 的 DataGridView 控件。 
- 对 System、System.Drawing 和 System.Windows.Forms 程序集的引用。 
可靠编程
为在使用非常大的数据集时获得最大可伸缩性,应该在使用相同样式的多个行、列或单元格之间共享 DataGridViewCellStyle 对象,而非单独设置每个元素的样式属性。 另外,应该创建共享行,并通过使用 DataGridViewRowCollection.SharedRow 属性访问它们。 有关更多信息,请参见 缩放 Windows 窗体 DataGridView 控件的最佳做法。
请参见
任务
如何:为 Windows 窗体 DataGridView 控件设置交替行样式
参考
DataGridViewBand.DefaultCellStyle
概念
Windows 窗体 DataGridView 控件中的单元格样式
缩放 Windows 窗体 DataGridView 控件的最佳做法