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

Answers

  • There is a property called EnableHeadersVisualStyles that needs to be set to false in order to get your custom settings to show up.
    Friday, June 09, 2006 10:03 PM
  • 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);
    
    }
    

    Friday, June 02, 2006 1:46 PM

All replies

  • 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);
    
    }
    

    Friday, June 02, 2006 1:46 PM
  • Thanks Galin :)

    Can I also get the header backcolor with varying shades?

    Friday, June 02, 2006 3:03 PM
  • 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.
    Thursday, June 08, 2006 5:59 PM
  • There is a property called EnableHeadersVisualStyles that needs to be set to false in order to get your custom settings to show up.
    Friday, June 09, 2006 10:03 PM
  • 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?

    Saturday, June 10, 2006 6:19 AM
  • 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.

    Thursday, June 22, 2006 2:14 PM