共用方式為


How to: Change the Border and Gridline Styles in the Windows Forms DataGridView Control

With the DataGridView control, you can customize the appearance of the control's border and gridlines to improve the user experience. You can modify the gridline color and the control border style in addition to the border styles for the cells within the control. You can also apply different cell border styles for ordinary cells, row header cells, and column header cells.

NoteNote

The gridline color is used only with the Single, SingleHorizontal, and SingleVertical values of the DataGridViewCellBorderStyle enumeration and the Single value of the DataGridViewHeaderBorderStyle enumeration. The other values of these enumerations use colors specified by the operating system. Additionally, when visual styles are enabled on Windows XP and the Windows Server 2003 family through the System.Windows.Forms.Application.EnableVisualStyles method, the GridColor property value is not used.

To change the gridline color programmatically

  • Set the GridColor property.

    Me.dataGridView1.GridColor = Color.BlueViolet
    
    this.dataGridView1.GridColor = Color.BlueViolet;
    

To change the border style of the entire DataGridView control programmatically

  • Set the BorderStyle property to one of the BorderStyle enumeration values.

    Me.dataGridView1.BorderStyle = BorderStyle.Fixed3D
    
    this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;
    

To change the border styles for DataGridView cells programmatically

  • Set the CellBorderStyle, RowHeadersBorderStyle, and ColumnHeadersBorderStyle properties.

    With Me.dataGridView1
        .CellBorderStyle = DataGridViewCellBorderStyle.None
        .RowHeadersBorderStyle = _
            DataGridViewHeaderBorderStyle.Single
        .ColumnHeadersBorderStyle = _
            DataGridViewHeaderBorderStyle.Single
    End With
    
    this.dataGridView1.CellBorderStyle =
        DataGridViewCellBorderStyle.None;
    this.dataGridView1.RowHeadersBorderStyle =
        DataGridViewHeaderBorderStyle.Single;
    this.dataGridView1.ColumnHeadersBorderStyle =
        DataGridViewHeaderBorderStyle.Single;
    

Example

Private Sub SetBorderAndGridlineStyles()

    With Me.dataGridView1
        .GridColor = Color.BlueViolet
        .BorderStyle = BorderStyle.Fixed3D
        .CellBorderStyle = DataGridViewCellBorderStyle.None
        .RowHeadersBorderStyle = _
            DataGridViewHeaderBorderStyle.Single
        .ColumnHeadersBorderStyle = _
            DataGridViewHeaderBorderStyle.Single
    End With

End Sub
private void SetBorderAndGridlineStyles()
{
    this.dataGridView1.GridColor = Color.BlueViolet;
    this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;
    this.dataGridView1.CellBorderStyle =
        DataGridViewCellBorderStyle.None;
    this.dataGridView1.RowHeadersBorderStyle =
        DataGridViewHeaderBorderStyle.Single;
    this.dataGridView1.ColumnHeadersBorderStyle =
        DataGridViewHeaderBorderStyle.Single;
}

Compiling the Code

This example requires:

See Also

Reference

BorderStyle
System.Windows.Forms.DataGridView.BorderStyle
System.Windows.Forms.DataGridView.CellBorderStyle
System.Windows.Forms.DataGridView.ColumnHeadersBorderStyle
System.Windows.Forms.DataGridView.GridColor
System.Windows.Forms.DataGridView.RowHeadersBorderStyle
DataGridViewCellBorderStyle
DataGridViewHeaderBorderStyle

Other Resources

Basic Formatting and Styling in the Windows Forms DataGridView Control