다음을 통해 공유


How to: Set Font and Color Styles in the Windows Forms DataGridView Control

You can specify the visual appearance of cells within a DataGridView control by setting properties of the DataGridViewCellStyle class. You can retrieve instances of this class from various properties of the DataGridView class and its companion classes, or you can instantiate DataGridViewCellStyle objects for assignment to these properties.

The following procedures demonstrate basic customization of cell appearance using the DefaultCellStyle property. Every cell in the control inherits the styles specified through this property unless they are overridden at the column, row, or cell level. For an example of style inheritance, see How to: Set Default Cell Styles for the Windows Forms DataGridView Control. For information about additional uses of the DataGridViewCellStyle class, see the topics listed in the See Also section.

There is extensive support for this task in Visual Studio. How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer
How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer
How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer
How to: Set Default Cell Styles and Data Formats for the Windows Forms DataGridView Control Using the Designer

To specify the font used by DataGridView cells

  • Set the Font property of a DataGridViewCellStyle. The following code example uses the System.Windows.Forms.DataGridView.DefaultCellStyle property to set the font for the entire control.

    Me.dataGridView1.DefaultCellStyle.Font = New Font("Tahoma", 15)
    
    this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 15);
    

To specify the foreground and background colors of DataGridView cells

  • Set the ForeColor and BackColor properties of a DataGridViewCellStyle. The following code example uses the System.Windows.Forms.DataGridView.DefaultCellStyle property to set these styles for the entire control.

    Me.dataGridView1.DefaultCellStyle.ForeColor = Color.Blue
    Me.dataGridView1.DefaultCellStyle.BackColor = Color.Beige
    
    this.dataGridView1.DefaultCellStyle.ForeColor = Color.Blue;
    this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
    

To specify the foreground and background colors of selected DataGridView cells

  • Set the SelectionForeColor and SelectionBackColor properties of a DataGridViewCellStyle. The following code example uses the System.Windows.Forms.DataGridView.DefaultCellStyle property to set these styles for the entire control.

    Me.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow
    Me.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black
    
    this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;
    this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
    

Example

Private Sub SetFontAndColors()

    With Me.dataGridView1.DefaultCellStyle
        .Font = New Font("Tahoma", 15)
        .ForeColor = Color.Blue
        .BackColor = Color.Beige
        .SelectionForeColor = Color.Yellow
        .SelectionBackColor = Color.Black
    End With

End Sub
private void SetFontAndColors()
{
    this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 15);
    this.dataGridView1.DefaultCellStyle.ForeColor = Color.Blue;
    this.dataGridView1.DefaultCellStyle.BackColor = Color.Beige;
    this.dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;
    this.dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black;
}

Compiling the Code

This example requires:

Robust Programming

For maximum scalability, you should share DataGridViewCellStyle objects across multiple rows, columns, or cells that use the same styles, rather than setting the style properties for each element separately. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

See Also

Reference

System.Windows.Forms.DataGridView.DefaultCellStyle
DataGridViewCellStyle

Concepts

Cell Styles in the Windows Forms DataGridView Control

Other Resources

Basic Formatting and Styling in the Windows Forms DataGridView Control