Hot to change specific column header color only in datagridview?

38,875

Solution 1

First in your DataGridView you need to set EnableHeadersVisualStyles to false. After you've done that you can set the individual header style on each column.

        DataGridViewColumn dataGridViewColumn = dataGridView1.Columns[0];
        dataGridViewColumn.HeaderCell.Style.BackColor = Color.Magenta;
        dataGridViewColumn.HeaderCell.Style.ForeColor = Color.Yellow;

Solution 2

Do it in this way

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
{
     col.Name = "ColumnName";
     col.HeaderText = "HeaderName";
     col.DefaultCellStyle.ForeColor = Color.White;
     col.HeaderCell.Style.BackColor = Color.Red; //Column Header Color
     this.dataGridView1.Columns.Add(col);
}

Solution 3

Create a method name called SetUpDataGridView

private void SetUpDataGridView()
{
  dataGridView1.Columns[0].HeaderText = "Emp.Id";         
  dataGridView1.Columns[0].HeaderCell.Style.BackColor = Color.Chartreuse;
  dataGridView1.Columns[1].HeaderText = "Emp. Name";
  dataGridView1.Columns[1].HeaderCell.Style.BackColor = Color.Fuchsia;
}

Add the method in Form_Load. You can add different color for every header

Share:
38,875
hiFI
Author by

hiFI

Updated on July 05, 2022

Comments

  • hiFI
    hiFI almost 2 years

    Uses: VS 2005, C#, DataGridView, WinForms;

    I need to color the font/background of a particular column's Header portion. I see that it can only be done to the entire column list's header instead of a single column. Any help greatly appreciated.

  • B H
    B H over 8 years
    I was able to change font to bold without changing EnableHeadersVisualStyles to false using dgc.HeaderCell.Style.Font = new Font(dgc.HeaderCell.DataGridView.DefaultCellStyle.Font, FontStyle.Bold) where dgc is the DataGridViewColumn I'm trying to modify.
  • TaW
    TaW almost 8 years
    @B H: That works for the Font but not for the Colors.