如何:更改 Windows 窗体 DataGridView 控件中的边框和网格线的样式How to: Change the Border and Gridline Styles in the Windows Forms DataGridView Control
03/30/
本文内容
在 DataGridView 控件中,您可以自定义控件的边框和网格线的外观,以改善用户体验。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.
备注
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. 此外,当通过方法在 Windows XP 和 Windows Server 家族上启用视觉样式时 Application.EnableVisualStyles , GridColor 不使用属性值。Additionally, when visual styles are enabled on Windows XP and the Windows Server family through the Application.EnableVisualStyles method, the GridColor property value is not used.
以编程方式更改网格线颜色To change the gridline color programmatically
Set the GridColor property.
this.dataGridView1.GridColor = Color.BlueViolet;Me.dataGridView1.GridColor = Color.BlueViolet
以编程方式更改整个 DataGridView 控件的边框样式To change the border style of the entire DataGridView control programmatically
Set the BorderStyle property to one of the BorderStyle enumeration values.
this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;Me.dataGridView1.BorderStyle = BorderStyle.Fixed3D
以编程方式更改 DataGridView 单元格的边框样式To change the border styles for DataGridView cells programmatically
this.dataGridView1.CellBorderStyle =
DataGridViewCellBorderStyle.None;
this.dataGridView1.RowHeadersBorderStyle =
DataGridViewHeaderBorderStyle.Single;
this.dataGridView1.ColumnHeadersBorderStyle =
DataGridViewHeaderBorderStyle.Single;With Me.dataGridView1
.CellBorderStyle = DataGridViewCellBorderStyle.None
.RowHeadersBorderStyle = _
DataGridViewHeaderBorderStyle.Single
.ColumnHeadersBorderStyle = _
DataGridViewHeaderBorderStyle.Single
End With
示例Example
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;
}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
编译代码Compiling the Code
此示例需要:This example requires:
另请参阅See also
datagrid设置单元格边框_更改 DataGridView 控件中的边框和网格线样式 - Windows Forms .NET Framework | Microsoft Docs...