如何:设置 Windows 窗体 DataGridView 控件的默认单元格样式How to: Set Default Cell Styles for the Windows Forms DataGridView Control

借助 DataGridView 控件,可以指定整个控件以及特定列和行的默认单元格样式。With the DataGridView control, you can specify default cell styles for the entire control and for specific columns and rows. 这些默认设置依次向下筛选:从控件级别到列级别,然后到行级别,再到单元格级别。These defaults filter down from the control level to the column level, then to the row level, then to the cell level. 如果未在单元格级别设置特定 DataGridViewCellStyle 属性,则使用行级别的默认属性设置。If a particular DataGridViewCellStyle property is not set at the cell level, the default property setting at the row level is used. 如果在行级别也未设置该属性,则使用默认列设置。If the property is also not set at the row level, the default column setting is used. 最后,如果在列级别也未设置该属性,那么则使用默认的 DataGridView 设置。Finally, if the property is also not set at the column level, the default DataGridView setting is used. 借助此设置,可以避免必须重复多个级别的属性设置。With this setting, you can avoid having to duplicate the property settings at multiple levels. 在每个级别,只需指定与其上级别不同的样式。At each level, simply specify the styles that differ from the levels above it. 有关详细信息,请参阅Windows 窗体 DataGridView 控件中的单元格样式For more information, see Cell Styles in the Windows Forms DataGridView Control.

Visual Studio 中对此任务提供广泛支持。There is extensive support for this task in Visual Studio. 另请参阅如何:设置 Windows 窗体 DataGridView 控件使用设计器的默认单元格样式和数据格式Also see How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer.

以编程方式设置默认单元格样式To set the default cell styles programmatically

  1. 设置通过 DataGridView.DefaultCellStyle 属性检索的 DataGridViewCellStyle 的属性。Set the properties of the DataGridViewCellStyle retrieved through the DataGridView.DefaultCellStyle property.

    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 对象。Create and initialize new DataGridViewCellStyle objects for use by multiple rows and columns.

    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 属性。Set the DefaultCellStyle property of specific rows and columns.

    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
    

示例Example

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

编译代码Compiling the Code

此示例需要:This example requires:

可靠编程Robust Programming

若要在处理很大的数据集时实现最大可伸缩性,则应在多个使用相同样式的行、列或单元格之间共享 DataGridViewCellStyle 对象,而不是分别设置单个元素的样式属性。To achieve maximum scalability when you work with very large data sets, you should share DataGridViewCellStyle objects across multiple rows, columns, or cells that use the same styles, rather than set the style properties for individual elements separately. 此外,应创建共享行并使用 DataGridViewRowCollection.SharedRow 属性对其进行访问。Additionally, you should create shared rows and access them by using the DataGridViewRowCollection.SharedRow property. 有关详细信息,请参阅缩放 Windows 窗体 DataGridView 控件的最佳做法For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

请参阅See also