Gopal
Gopal

Reputation: 11982

How to change the datagridView Header color

Now the datagridView Header Background color is showing in Gray. I want to change to differenct color.

I Changed the background color in ColumnHeaderDefaultCellStyle, but nothing changed.

How to do this.

Upvotes: 8

Views: 79420

Answers (3)

Georg
Georg

Reputation: 414

Also, if you are trying to set the color (back or fore)color or other properties of the individual column's header (not all at once) use

datagridview.Columns(e.ColumnIndex).HeaderCell.Style.BackColor = color.cyan
datagridview.Columns(e.ColumnIndex).HeaderCell.Style.(ForeColor or Font or Alignment etc) = whatever

where e.ColumnIndex was taken from the EventArgs of your Event, but you can alter accordingly.

Upvotes: 6

codeGEN
codeGEN

Reputation: 714

Set the property EnableHeadersVisualStyles to False, then change the ColumnHeaderDefaultCellStyle background color to the color that you desire. You will be able to see the changes in the designer itself.

Upvotes: 29

In datagridView you can change the Header color by using DataGridViewCellStyle, see the following code

       ' Set the selection background color for all the cells.
    dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
    dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black

    ' Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
    ' value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
    dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty

    ' Set the background color for all rows and for alternating rows. 
    ' The value for alternating rows overrides the value for all rows. 
    dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray
    dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray

    ' Set the row and column header styles.
    dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
    dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Black
    dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Black

EDIT:

Using the DataGridViewCellStyle, your header color will changes but a seperator for columns in the header section will not appear. So, heres a overrided event of OnPaint Event Handler have a look at this

Upvotes: 2

Related Questions