简体   繁体   English

如何更改datagridView标头颜色

[英]How to change the datagridView Header color

Now the datagridView Header Background color is showing in Gray. 现在,datagridView标题背景颜色以灰色显示。 I want to change to differenct color. 我想换成不同的颜色。

I Changed the background color in ColumnHeaderDefaultCellStyle , but nothing changed. 我在ColumnHeaderDefaultCellStyle更改了背景颜色,但没有任何改变。

How to do this. 这个怎么做。

Set the property EnableHeadersVisualStyles to False , then change the ColumnHeaderDefaultCellStyle background color to the color that you desire. 设置该属性EnableHeadersVisualStylesFalse ,然后更改ColumnHeaderDefaultCellStyle背景颜色,你的愿望颜色。 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. 其中e.ColumnIndex取自Event的EventArgs,但您可以相应地更改。

In datagridView you can change the Header color by using DataGridViewCellStyle , see the following code 在datagridView中,您可以使用DataGridViewCellStyle更改标题颜色,请参阅以下代码

       ' 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. 使用DataGridViewCellStyle,标题颜色将更改,但标题部分中的列的分隔符将不会出现。 So, heres a overrided event of OnPaint Event Handler have a look at this 所以,继承人OnPaint事件处理程序的一个重要事件看看这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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