How to change the color of winform DataGridview header?

C#.NetWinformsDatagridview

C# Problem Overview


I have tried to do it without success.

Is it possible ?

C# Solutions


Solution 1 - C#

The way to do this is to set the EnableHeadersVisualStyles flag for the data grid view to False, and set the background colour via the ColumnHeadersDefaultCellStyle.BackColor property. For example, to set the background colour to blue, use the following (or set in the designer if you prefer):

_dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
_dataGridView.EnableHeadersVisualStyles = false;

If you do not set the EnableHeadersVisualStyles flag to False, then the changes you make to the style of the header will not take effect, as the grid will use the style from the current users default theme. The MSDN documentation for this property is here.

Solution 2 - C#

dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;

Solution 3 - C#

It can be done.

From the designer: Select your DataGridView Open the Properties Navigate to ColumnHeaderDefaultCellStype Hit the button to edit the style.

You can also do it programmatically:

dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Purple;

Hope that helps!

Solution 4 - C#

If you want to change a color to single column try this:

 dataGridView1.EnableHeadersVisualStyles = false;
 dataGridView1.Columns[0].HeaderCell.Style.BackColor = Color.Magenta;
 dataGridView1.Columns[1].HeaderCell.Style.BackColor = Color.Yellow;

Solution 5 - C#

This works for me. thanks!

dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue; 
dataGridView1.Columns[0].HeaderCell.Style.BackColor = Color.Magenta;
dataGridView1.Columns[1].HeaderCell.Style.BackColor = Color.Yellow;

enter image description here

Solution 6 - C#

Always, set dataGridView.EnableHeadersVisualStyles = false; before styling the column header or row header. then only , the custom styles you made will be applied to row and column header.

     DataGridViewCellStyle column_header_cell_style = new DataGridViewCellStyle();
                column_header_cell_style.BackColor = Color.LightSalmon;
                column_header_cell_style.ForeColor = Color.Black;
                column_header_cell_style.SelectionBackColor = Color.Chocolate;
                column_header_cell_style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                column_header_cell_style.Font = new Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Pixel);


this.dataGridView.ColumnHeadersDefaultCellStyle = column_header_cell_style;

Solution 7 - C#

dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprogrammernoviceView Question on Stackoverflow
Solution 1 - C#Rhys JonesView Answer on Stackoverflow
Solution 2 - C#mahvashView Answer on Stackoverflow
Solution 3 - C#Brandon View Answer on Stackoverflow
Solution 4 - C#daniele3004View Answer on Stackoverflow
Solution 5 - C#quickbrownView Answer on Stackoverflow
Solution 6 - C#Vintage CoderView Answer on Stackoverflow
Solution 7 - C#Alok Kumar SahooView Answer on Stackoverflow