Can I change the BackColor of the DataGridView's Column Header and Row Header (C# VS 2005)

Please help me-out with this issue.


Answer this question

Can I change the BackColor of the DataGridView's Column Header and Row Header (C# VS 2005)

  • Colby

    check DataGridView.ColumnHeadersDefaultCellStyle Property

    http://msdn2.microsoft.com/en-us/system.windows.forms.datagridview.columnheadersdefaultcellstyle.aspx

     

     MSDN.COM wrote:

    private void SetUpDataGridView()
    {
      this.Controls.Add(dataGridView1);
      dataGridView1.ColumnCount = 5;
      DataGridViewCellStyle style = 
        dataGridView1.ColumnHeadersDefaultCellStyle;
      style.BackColor = Color.Navy;
      style.ForeColor = Color.White;
      style.Font = new Font(dataGridView1.Font, FontStyle.Bold);
    
      dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
      dataGridView1.Name = "dataGridView1";
      dataGridView1.Location = new Point(8, 8);
      dataGridView1.Size = new Size(500, 300);
      dataGridView1.AutoSizeRowsMode = 
        DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
      dataGridView1.ColumnHeadersBorderStyle = 
        DataGridViewHeaderBorderStyle.Raised;
      dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
      dataGridView1.GridColor = SystemColors.ActiveBorder;
      dataGridView1.RowHeadersVisible = false;
    
      dataGridView1.Columns[0].Name = "Release Date";
      dataGridView1.Columns[1].Name = "Track";
      dataGridView1.Columns[1].DefaultCellStyle.Alignment = 
        DataGridViewContentAlignment.MiddleCenter;
      dataGridView1.Columns[2].Name = "Title";
      dataGridView1.Columns[3].Name = "Artist";
      dataGridView1.Columns[4].Name = "Album";
    
      // Make the font italic for row four.
      dataGridView1.Columns[4].DefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Italic);
    
      dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      dataGridView1.MultiSelect = false;
    
      dataGridView1.BackgroundColor = Color.Honeydew;
    
      dataGridView1.Dock = DockStyle.Fill;
    
      dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
      dataGridView1.CellParsing += new DataGridViewCellParsingEventHandler(dataGridView1_CellParsing);
      addNewRowButton.Click += new EventHandler(addNewRowButton_Click);
      deleteRowButton.Click += new EventHandler(deleteRowButton_Click);
      ledgerStyleButton.Click += new EventHandler(ledgerStyleButton_Click);
      dataGridView1.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
    
    }
    



  • CiaranODonnell

    Thank you Preston, that solved my problem. But even in my 2000 server machine, EnableHeadersVisualStyles property value was 'True', then how did it worked, just want to know why is this needed in XP



  • maheshbhide

    Galin, I tried with ColumnHeadersDefaultCellStyle and it worked fine in Windows 2000 server machine but the same code didn't worked in Windows XP machine. Plz. help me with this issue.

  • cc3078

    There is a property called EnableHeadersVisualStyles that needs to be set to false in order to get your custom settings to show up.

  • David So

    Thanks Galin :)

    Can I also get the header backcolor with varying shades



  • dneigler

    Hi,

    How do i give my DataGridView an overall Header. It is currently the same as this example with row and column header but i want to give it an overall header. such as "CDs" - in your example.

    The previous way to do it was using CaptionText for a Datagrid but i cant find a way with DataGridView.

    Any help would be much appreciated.

    Thanks.


  • Can I change the BackColor of the DataGridView's Column Header and Row Header (C# VS 2005)