2011-10-24 102 views
4

现在datagridView标题背景颜色显示为灰色。我想改变颜色差异 。如何更改datagridView标题颜色

I更改了ColumnHeaderDefaultCellStyle中的背景颜色,但没有任何更改。

如何做到这一点。

+1

dublicate? http://stackoverflow.com/questions/3921497/setting-gridview-header-color –

+0

@Soner,我使用基于Windows的应用程序 – Gopal

+1

@Gopal,那么你应该简单地给dataGridView而不是gridview。 –

回答

1

在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 

编辑:

使用的DataGridViewCellStyle,你的头颜色会变化,但在标题为列分隔符部分不会出现。因此,继承人的OnPaint事件处理程序的overrided事件看看this

+0

我正在使用基于Windows的应用程序..... – Gopal

+0

@Gopal现在提交 –

+0

颜色工作正常,但列标题分隔线不显示... – Gopal

16

设置属性EnableHeadersVisualStylesFalse,然后更改ColumnHeaderDefaultCellStyle背景颜色,你的愿望颜色。您将能够看到设计师本身的变化。