如何更改datagridView标头颜色

Gop*_*pal 4 vb.net visual-studio winforms

现在,datagridView标题背景颜色以灰色显示.我想换成不同的颜色.

我改变了背景颜色ColumnHeaderDefaultCellStyle,但没有改变.

这该怎么做.

cod*_*GEN 19

将属性设置EnableHeadersVisualStylesFalse,然后将ColumnHeaderDefaultCellStyle背景颜色更改为您想要的颜色.您将能够看到设计师本身的变化.


Geo*_*org 6

另外,如果您尝试设置单个列标题的颜色(后或前)颜色或其他属性(不是一次全部),请使用

datagridview.Columns(e.ColumnIndex).HeaderCell.Style.BackColor = color.cyan
datagridview.Columns(e.ColumnIndex).HeaderCell.Style.(ForeColor or Font or Alignment etc) = whatever
Run Code Online (Sandbox Code Playgroud)

其中 e.ColumnIndex 取自您的事件的 EventArgs,但您可以相应地进行更改。


Sai*_*ala 2

在 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
Run Code Online (Sandbox Code Playgroud)

编辑:

使用 DataGridViewCellStyle,标题颜色将发生变化,但标题部分中的列分隔符将不会出现。所以,这是 OnPaint 事件处理程序的一个重写事件,看看这个