WinForms
DataGridView
Header Customization
UI Design
C# Programming

How to change the color of winform DataGridview header?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Changing the header color of a WinForms DataGridView is mostly a styling task, but one property often blocks the expected result. If the system visual styles remain enabled, Windows can override your custom header colors and make it look as if your code did nothing.

Disable Visual Styles for the Header First

The most important setting is EnableHeadersVisualStyles. By default, it is true, which lets the operating system paint the headers using the current theme. When that happens, BackColor and ForeColor settings on the header style may be ignored.

Set it to false before applying your custom colors:

csharp
1using System;
2using System.Drawing;
3using System.Windows.Forms;
4
5public class MainForm : Form
6{
7    private readonly DataGridView grid = new DataGridView();
8
9    public MainForm()
10    {
11        grid.Dock = DockStyle.Fill;
12        grid.EnableHeadersVisualStyles = false;
13
14        grid.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
15        grid.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
16        grid.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.Navy;
17        grid.ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.White;
18
19        grid.Columns.Add("name", "Name");
20        grid.Columns.Add("score", "Score");
21        grid.Rows.Add("Ana", 92);
22        grid.Rows.Add("Bob", 87);
23
24        Controls.Add(grid);
25    }
26
27    [STAThread]
28    public static void Main()
29    {
30        Application.EnableVisualStyles();
31        Application.Run(new MainForm());
32    }
33}

If you remember only one thing from this topic, remember that property. Most failed attempts come from leaving visual styles enabled.

Style Column Headers and Row Headers Separately

DataGridView has separate style objects for column headers and row headers. That lets you color them independently if your design calls for it.

csharp
1grid.EnableHeadersVisualStyles = false;
2
3grid.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkSlateBlue;
4grid.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
5grid.ColumnHeadersDefaultCellStyle.Font = new Font("Segoe UI", 10, FontStyle.Bold);
6
7grid.RowHeadersDefaultCellStyle.BackColor = Color.Gainsboro;
8grid.RowHeadersDefaultCellStyle.ForeColor = Color.Black;

This separation is useful when you want a bold column header bar but a more neutral row header area.

You can also control alignment:

csharp
grid.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

That small change often improves the appearance more than color alone.

Apply a Consistent Selection Style

Selection colors can make a styled header look broken if they differ from the base header colors. Header cells do not behave exactly like normal data cells, so it is a good idea to set both the regular and selection colors explicitly.

csharp
1grid.ColumnHeadersDefaultCellStyle.BackColor = Color.Teal;
2grid.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
3grid.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.Teal;
4grid.ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.White;

Without those selection properties, the header can flash to system colors when focused or clicked, which makes the styling feel inconsistent.

Designer and Code-Behind Both Work

You can set these values in the Visual Studio designer or in code. For small apps, the designer is fine. For reusable controls or team projects, code-behind is often better because the styling is explicit, reviewable, and easy to apply in one place.

For example, a helper method keeps the grid setup easy to reuse:

csharp
1private static void ApplyHeaderStyle(DataGridView grid)
2{
3    grid.EnableHeadersVisualStyles = false;
4    grid.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(45, 62, 80);
5    grid.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
6    grid.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.FromArgb(45, 62, 80);
7    grid.ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.White;
8    grid.ColumnHeadersDefaultCellStyle.Font = new Font("Segoe UI", 9, FontStyle.Bold);
9}

That makes it much easier to keep multiple grids visually consistent.

Refresh the Grid After Major Style Changes

Most style updates apply immediately, but if you make several changes after the grid is already bound and visible, forcing a repaint can help:

csharp
grid.Refresh();

This is not always required, but it can make debugging easier when you are experimenting with style changes at runtime.

Common Pitfalls

The biggest pitfall is forgetting to set EnableHeadersVisualStyles to false, which allows the OS theme to override your header styling. Another common issue is styling only ColumnHeadersDefaultCellStyle and then wondering why row headers still use system colors. Developers also sometimes set the header background but forget the selection colors, which causes unexpected color changes during interaction. Finally, if a project mixes designer styling and runtime styling, the order of assignment can make the final result harder to predict.

Summary

  • Set EnableHeadersVisualStyles to false or your custom header colors may be ignored.
  • Use ColumnHeadersDefaultCellStyle to style column headers.
  • Use RowHeadersDefaultCellStyle separately if you also want to style row headers.
  • Set selection colors so the header keeps a consistent appearance when focused.
  • Prefer a helper method or centralized setup if multiple grids should share the same look.

Course illustration
Course illustration

All Rights Reserved.