niaomingjian
niaomingjian

Reputation: 3742

How to set header cell borders' color of datagridview

GridColor property of DataGridView can set the color of the grid lines separating the cells of the DataGridView(i.e. borders of every cell).

But it can't change the grid lines' color of header cells(i.e. borders of header cells).
How can I achieve this?
Is there a property that I can use to accomplish this?

1.image of using GridColor property
using <code>GridColor</code> property
2.image of My requirement
enter image description here

As TnTinMn said, the GridColor property only affects cells with CellBorderStyle= DataGridViewCellBorderStyle.Single.
The result is like the following image.
It can't affect the grid lines seperating the header cells.

3.image of using CellBorderStyle property enter image description here

After another try, I succeed.
4.image of using `EnableHeadersVisualStyles = false' enter image description here

(Solution)To summarize:
1.Using GridColor to set the color of grid lines.
2.Set CellBorderStyle,ColumnHeadersBorderStyle,RowHeadersBorderStyle to Single.
3.Set EnableHeadersVisualStyles to false.

Upvotes: 14

Views: 8816

Answers (2)

Marsd3q
Marsd3q

Reputation: 70

Coming in late, but if you want to keep EnableHeadersVisualStyles = true, you may change AdvancedColumnHeaderBorderStyle, AdvancedRowHeadersBorderStyle, AdvancedCellBorderStyle side properties. To solve @niaomingjian example, you would need to change these as:

dgv.AdvancedColumnHeadersBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
dgv.AdvancedColumnHeadersBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
dgv.AdvancedColumnHeadersBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single;
dgv.AdvancedColumnHeadersBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;

dgv.AdvancedCellBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
dgv.AdvancedCellBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
dgv.AdvancedCellBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single;
dgv.AdvancedCellBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;

dgv.AdvancedRowHeadersBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
dgv.AdvancedRowHeadersBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
dgv.AdvancedRowHeadersBorderStyle.Right = DataGridViewAdvancedCellBorderStyle.Single;
dgv.AdvancedRowHeadersBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.Single;

Setting AdvancedColumnHeaderBorderStyle.All, AdvancedRowHeadersBorderStyle.All or AdvancedCellBorderStyle.All to a single style, seems to work differently so you need to adjust every side separately to trigger such styling.

Upvotes: 0

Chalky
Chalky

Reputation: 1642

Do the 3 things you listed, and then:

  1. Set CellBorderStyle = DataGridViewCellBorderStyle.Raised - this will allow your colouring to show up in the header only, but because you've now set the body cell border to a 3D type (i.e. raised, sunken etc, not single or none etc), the body's cell will have a system default styling (likely to be grey).

Example

Upvotes: 2

Related Questions