【发布时间】:2010-11-17 21:09:48
【问题描述】:
我尝试过没有成功。
有可能吗?
【问题讨论】:
标签: c# .net winforms datagridview
我尝试过没有成功。
有可能吗?
【问题讨论】:
标签: c# .net winforms datagridview
可以做到的。
来自设计师: 选择您的 DataGridView 打开属性 导航到 ColumnHeaderDefaultCellStype 点击按钮编辑样式。
您也可以通过编程方式进行:
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Purple;
希望有帮助!
【讨论】:
这样做的方法是将数据网格视图的EnableHeadersVisualStyles 标志设置为False,并通过ColumnHeadersDefaultCellStyle.BackColor 属性设置背景颜色。例如,要将背景颜色设置为蓝色,请使用以下内容(或根据需要在设计器中设置):
_dataGridView.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
_dataGridView.EnableHeadersVisualStyles = false;
如果您不将EnableHeadersVisualStyles 标志设置为False,那么您对标题样式所做的更改将不会生效,因为网格将使用当前用户默认主题中的样式。此属性的 MSDN 文档是 here。
【讨论】:
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
【讨论】:
如果您想将颜色更改为单列,请尝试以下操作:
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.Columns[0].HeaderCell.Style.BackColor = Color.Magenta;
dataGridView1.Columns[1].HeaderCell.Style.BackColor = Color.Yellow;
【讨论】:
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
【讨论】:
始终在设置列标题或行标题的样式之前设置dataGridView.EnableHeadersVisualStyles = false;。那么只有 ,您制作的自定义样式将应用于行和列标题。
DataGridViewCellStyle column_header_cell_style = new DataGridViewCellStyle();
column_header_cell_style.BackColor = Color.LightSalmon;
column_header_cell_style.ForeColor = Color.Black;
column_header_cell_style.SelectionBackColor = Color.Chocolate;
column_header_cell_style.Alignment = DataGridViewContentAlignment.MiddleCenter;
column_header_cell_style.Font = new Font("Tahoma", 18, FontStyle.Bold, GraphicsUnit.Pixel);
this.dataGridView.ColumnHeadersDefaultCellStyle = column_header_cell_style;
【讨论】: