C# 如何更改 winform DataGridview 标题的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1247800/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:12:23  来源:igfitidea点击:

How to change the color of winform DataGridview header?

c#.netwinformsdatagridview

提问by programmernovice

I have tried to do it without success.

我试图做到这一点,但没有成功。

Is it possible ?

是否可以 ?

采纳答案by Rhys Jones

The way to do this is to set the EnableHeadersVisualStylesflag for the data grid view to False, and set the background colour via the ColumnHeadersDefaultCellStyle.BackColorproperty. For example, to set the background colour to blue, use the following (or set in the designer if you prefer):

这样做的方法是EnableHeadersVisualStyles将数据网格视图的标志设置为False,并通过ColumnHeadersDefaultCellStyle.BackColor属性设置背景颜色。例如,要将背景颜色设置为蓝色,请使用以下内容(如果您愿意,也可以在设计器中设置):

_dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
_dataGridView.EnableHeadersVisualStyles = false;

If you do not set the EnableHeadersVisualStylesflag to False, then the changes you make to the style of the header will not take effect, as the grid will use the style from the current users default theme. The MSDN documentation for this property is here.

如果您不将该EnableHeadersVisualStyles标志设置为 False,则您对标题样式所做的更改将不会生效,因为网格将使用当前用户默认主题中的样式。此属性的 MSDN 文档在此处

回答by Brandon

It can be done.

可以办到。

From the designer: Select your DataGridView Open the Properties Navigate to ColumnHeaderDefaultCellStype Hit the button to edit the style.

从设计器中:选择您的 DataGridView 打开属性导航到 ColumnHeaderDefaultCellStype 点击按钮编辑样式。

You can also do it programmatically:

您也可以以编程方式执行此操作:

dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Purple;

Hope that helps!

希望有帮助!

回答by mahvash

dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;

回答by daniele3004

If you want to change a color to single column try this:

如果要将颜色更改为单列,请尝试以下操作:

 dataGridView1.EnableHeadersVisualStyles = false;
 dataGridView1.Columns[0].HeaderCell.Style.BackColor = Color.Magenta;
 dataGridView1.Columns[1].HeaderCell.Style.BackColor = Color.Yellow;

回答by Alok Kumar Sahoo

dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;

dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;