vb.net 如何更改 datagridView 标题颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7871569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the datagridView Header color
提问by Gopal
Now the datagridView Header Background color is showing in Gray. I want to change to differenct color.
现在 datagridView 标题背景颜色显示为灰色。我想更改为不同的颜色。
I Changed the background color in ColumnHeaderDefaultCellStyle
, but nothing changed.
我更改了 中的背景颜色ColumnHeaderDefaultCellStyle
,但没有任何改变。
How to do this.
这该怎么做。
采纳答案by Sai Kalyan Kumar Akshinthala
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. So, heres a overrided event of OnPaint Event Handler have a look at this
使用 DataGridViewCellStyle,您的标题颜色将更改,但标题部分中的列分隔符不会出现。所以,这里有一个 OnPaint 事件处理程序的覆盖事件,看看这个
回答by codeGEN
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.
将属性设置EnableHeadersVisualStyles
为False
,然后将ColumnHeaderDefaultCellStyle
背景颜色更改为您想要的颜色。您将能够看到设计器本身的变化。
回答by Georg
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 取自事件的 EventArgs,但您可以相应地进行更改。