C# 热仅在 datagridview 中更改特定列标题颜色?

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

Hot to change specific column header color only in datagridview?

c#winformsdatagridviewvisual-studio-2005c#-2.0

提问by hiFI

Uses: VS 2005, C#, DataGridView, WinForms;

用途:VS 2005、C#、DataGridView、WinForms;

I need to color the font/background of a particular column's Header portion. I see that it can only be done to the entire column list's header instead of a single column. Any help greatly appreciated.

我需要为特定列的标题部分的字体/背景着色。我看到它只能对整个列列表的标题而不是单个列进行。非常感谢任何帮助。

采纳答案by helgeheldre

First in your DataGridView you need to set EnableHeadersVisualStyles to false. After you've done that you can set the individual header style on each column.

首先在您的 DataGridView 中,您需要将 EnableHeadersVisualStyles 设置为 false。完成后,您可以在每列上设置单独的标题样式。

        DataGridViewColumn dataGridViewColumn = dataGridView1.Columns[0];
        dataGridViewColumn.HeaderCell.Style.BackColor = Color.Magenta;
        dataGridViewColumn.HeaderCell.Style.ForeColor = Color.Yellow;

回答by Ramgy Borja

Do it in this way

这样做

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
{
     col.Name = "ColumnName";
     col.HeaderText = "HeaderName";
     col.DefaultCellStyle.ForeColor = Color.White;
     col.HeaderCell.Style.BackColor = Color.Red; //Column Header Color
     this.dataGridView1.Columns.Add(col);
}

回答by John Britto

Create a method name called SetUpDataGridView

创建一个名为 SetUpDataGridView 的方法名称

private void SetUpDataGridView()
{
  dataGridView1.Columns[0].HeaderText = "Emp.Id";         
  dataGridView1.Columns[0].HeaderCell.Style.BackColor = Color.Chartreuse;
  dataGridView1.Columns[1].HeaderText = "Emp. Name";
  dataGridView1.Columns[1].HeaderCell.Style.BackColor = Color.Fuchsia;
}

Add the method in Form_Load. You can add different color for every header

在 Form_Load 中添加该方法。您可以为每个标题添加不同的颜色