【问题标题】:How to change the color of winform DataGridview header?如何更改winform DataGridview 标头的颜色?
【发布时间】:2010-11-17 21:09:48
【问题描述】:

我尝试过没有成功。

有可能吗?

【问题讨论】:

    标签: c# .net winforms datagridview


    【解决方案1】:

    可以做到的。

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

    您也可以通过编程方式进行:

    dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Purple;
    

    希望有帮助!

    【讨论】:

    • 这不起作用,因为您没有禁用 VisualStyles。由于 VisualStyles 设置为 true,因此对样式的任何更改都将不起作用。
    【解决方案2】:

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

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

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

    【讨论】:

    • +1。不仅有效,而且解释了为什么您必须执行这两个步骤。不错!
    • 我将它放在表单的加载事件中,以便在显示 datagridview 时设置列标题和行标题颜色。
    • 如果您只想更改某些特定的列标题:stackoverflow.com/questions/13067398/…
    • 一千次是的!
    • 感谢您提供这个解决方案。
    【解决方案3】:
    dataGridView1.EnableHeadersVisualStyles = false;
    dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;
    

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue;

        【讨论】:

          【解决方案6】:

          这对我有用。谢谢!

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

          【讨论】:

            【解决方案7】:

            始终在设置列标题或行标题的样式之前设置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;
            

            【讨论】:

              猜你喜欢
              • 2010-12-21
              • 1970-01-01
              • 1970-01-01
              • 2012-08-05
              • 2011-01-12
              • 2011-11-21
              • 1970-01-01
              相关资源
              最近更新 更多