| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Change the Border and Gridline Styles in the Windows Forms DataGridView Control |
03/30/2017 |
.net-framework |
|
article |
|
|
2f413c7a-4025-4171-8e3a-66ef908ea583 |
15 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Change the Border and Gridline Styles in the Windows Forms DataGridView Control
With the xref:System.Windows.Forms.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.
[!NOTE] The gridline color is used only with the xref:System.Windows.Forms.DataGridViewCellBorderStyle.Single, xref:System.Windows.Forms.DataGridViewCellBorderStyle.SingleHorizontal, and xref:System.Windows.Forms.DataGridViewCellBorderStyle.SingleVertical values of the xref:System.Windows.Forms.DataGridViewCellBorderStyle enumeration and the xref:System.Windows.Forms.DataGridViewHeaderBorderStyle.Single value of the xref:System.Windows.Forms.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 xref:System.Windows.Forms.Application.EnableVisualStyles%2A?displayProperty=nameWithType method, the xref:System.Windows.Forms.DataGridView.GridColor%2A property value is not used.
To change the gridline color programmatically
-
Set the xref:System.Windows.Forms.DataGridView.GridColor%2A property.
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#031] [!code-vbSystem.Windows.Forms.DataGridViewMisc#031]
To change the border style of the entire DataGridView control programmatically
-
Set the xref:System.Windows.Forms.DataGridView.BorderStyle%2A property to one of the xref:System.Windows.Forms.BorderStyle enumeration values.
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#032] [!code-vbSystem.Windows.Forms.DataGridViewMisc#032]
To change the border styles for DataGridView cells programmatically
-
Set the xref:System.Windows.Forms.DataGridView.CellBorderStyle%2A, xref:System.Windows.Forms.DataGridView.RowHeadersBorderStyle%2A, and xref:System.Windows.Forms.DataGridView.ColumnHeadersBorderStyle%2A properties.
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#033] [!code-vbSystem.Windows.Forms.DataGridViewMisc#033]
Example
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#030] [!code-vbSystem.Windows.Forms.DataGridViewMisc#030]
Compiling the Code
This example requires:
-
A xref:System.Windows.Forms.DataGridView control named
dataGridView1. -
References to the xref:System?displayProperty=nameWithType, xref:System.Windows.Forms?displayProperty=nameWithType, and xref:System.Drawing?displayProperty=nameWithType assemblies.
See Also
xref:System.Windows.Forms.BorderStyle
xref:System.Windows.Forms.DataGridView.BorderStyle%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView.CellBorderStyle%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView.ColumnHeadersBorderStyle%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView.GridColor%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView.RowHeadersBorderStyle%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewCellBorderStyle
xref:System.Windows.Forms.DataGridViewHeaderBorderStyle
Basic Formatting and Styling in the Windows Forms DataGridView Control