简体   繁体   中英

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM