如何:设置 Windows 窗体 DataGridView 控件的默认单元格样式

借助 DataGridView 控件,可以指定整个控件以及特定列和行的默认单元格样式。 这些默认设置依次向下筛选:从控件级别到列级别,然后到行级别,再到单元格级别。 如果未在单元格级别设置特定 DataGridViewCellStyle 属性,则使用行级别的默认属性设置。 如果在行级别也未设置该属性,则使用默认列设置。 最后,如果在列级别也未设置该属性,那么则使用默认的 DataGridView 设置。 借助此设置,可以避免必须重复多个级别的属性设置。 在每个级别,只需指定与其上级别不同的样式。 有关详细信息,请参阅 Windows 窗体 DataGridView 控件中的单元格样式

Visual Studio 中对此任务提供广泛支持。 另请参阅如何:使用设计器设置 Windows 窗体 DataGridView 控件的默认单元格样式和数据格式

以编程方式设置默认单元格样式

  1. 设置通过 DataGridView.DefaultCellStyle 属性检索的 DataGridViewCellStyle 的属性。

    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. 创建和初始化供多个行和列使用的新 DataGridViewCellStyle 对象。

    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 属性。

    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
    

示例

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

编译代码

此示例需要:

可靠编程

若要在处理很大的数据集时实现最大可伸缩性,则应在多个使用相同样式的行、列或单元格之间共享 DataGridViewCellStyle 对象,而不是分别设置单个元素的样式属性。 此外,应创建共享行并使用 DataGridViewRowCollection.SharedRow 属性对其进行访问。 有关详细信息,请参阅 缩放 Windows 窗体 DataGridView 控件的最佳做法

另请参阅