24

I am trying to make the column headers of my DataGridView bold, in Visual Studio 2008.

Every time I change my ColumnHeadersDefaultCellStyle to Calibri 9.75pt bold, using the properties box, the next time I reopen the saved form, the ColumnHeadersDefaultCellStyle has reverted to Calibri 9.75 without bold.

My form's font is Calibri 9.75 without bold, as is my default cell style, but I should be able to override the default cell style with my ColumnHeader style right?

I can solve this problem programmatically by setting the style when the form is shown, but we would like to have the Visual Studio designer show the bolded headers, so we can layout the columns appropriately for the space taken up by bold header text.

In addition, the actual designer file specifies that the ColumnHeadersDefaultCellStyle is bold, even though the designer interface says it is not bold.

dataGridViewCellStyle1.Font = new System.Drawing.Font("Calibri", 9.75F,  
    System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;

this.receiptDetailView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
svinja
  • 5,495
  • 5
  • 25
  • 43
Tony Peterson
  • 20,522
  • 15
  • 48
  • 66
  • I have the same problem. ColumnHeadersDefaultCellStyle font keeps reverting to non-bold. When I recompile the project, it changes from Bold to Regular in the designer code on its own. Have you maybe found a solution? – svinja Jul 30 '12 at 07:48
  • Closing/reopening the file also reverts it to non-bold. When I change to bold and save, I can actually see FontStyle.Bold in the designer code, but it changes on its own when reloaded or compiled. – svinja Jul 30 '12 at 07:59
  • http://speedy.sh/KsREw/nobold.zip here is a basic project where this problem is apparent. Things done: added DGV to the form, added columns to the DGV, changed the form font to regular arial, changed the DGV columnheadersdefaultcellstyle font to bold arial, disabled visual styles. – svinja Aug 06 '12 at 07:40

13 Answers13

27

Have you tried check EnableHeadersVisualStyles value?

According to MSDN:

If visual styles are enabled and EnableHeadersVisualStyles is set to true, all header cells except the TopLeftHeaderCell are painted using the current theme and the ColumnHeadersDefaultCellStyle values are ignored.

Oleg Sakharov
  • 1,127
  • 2
  • 20
  • 19
26

It is a bug, although Microsoft would probably try to call it a feature. The DataGridView header cells are supposed to inherit the current theme only if EnableHeadersVisualStyles is set to TRUE, and use the settings in ColumnHeaderDefaultCellStyles if it is false. But the DGV ignores EnableHeadersVisualStyles and always inherits the font of the parent container it resides in.

Both rutlean's and Nico Engler suggestions will work. Here is what I always do as a standard practice: Put your DGV in a panel (depending on your application, you might want to set the Dock property to fill. Then set the Panel's font to your desired settings. Your DGV will now always inherit that setting.

John
  • 271
  • 3
  • 3
3

I found a workaround where just editing the XXXX.Designer.cs with following code does the trick.

this.receiptDetailView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.receiptDetailView.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Bold);
perilbrain
  • 7,961
  • 2
  • 27
  • 35
  • Ah sorry, I forgot to mention, I am using Visual Studio 2010. In VS2010, this does not work. – svinja Aug 07 '12 at 09:26
  • @svinja I tested this on visual studio 2010 ultimate and even setting values in property panel worked.copy the project and paste it somewhere else.delete all unnecessary files like .sdf etc and reload the project.Hope this wont let the bug remain. – perilbrain Aug 07 '12 at 09:29
2

It seems that this is a bug, though I am not sure why it happens. I have tested it in every possible way and the value is overriden by the parent control value regardless of whether it is set or not. This is the opposite of how every other WinForms (or any other UI framework) control works, and doesn't make any sense. I have also tested various other controls and have not found another case where this happens.

The ColumnHeadersDefaultCellStyle Font only matters if the Font property is not set on the parent control (form in this case).

I am giving the bounty to the most upvoted answer but that is not what's going on here.

The "solution" to this that I've been using is to set the font again in the form load event, however this is not a perfect solution since such code doesn't belong there.

svinja
  • 5,495
  • 5
  • 25
  • 43
2

I ran into this same issue. However, my dataGridView is located in a groupbox. On a restart of VS 2010, the dataGridView fonts will always be whatever the groupBox is set to. Definitely a bug I would like.

rutlean
  • 21
  • 1
2

I resolved this problem by adding a frame. For me, the datagridview was inside a groupbox (although a few other container types did the same).

Resolved by putting a panel inside the groupbox, set the appropriate font to that panel, put the datagridview inside that panel and by default it inherits the fonts.

I am using VS2010

1

The answer is actually pretty simple.

You set a Font Style to Form1 [Arial; 8,25pt]. Lets see the designer :

private void InitializeComponent()
    {
        System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
        dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
        dataGridViewCellStyle1.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
        dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
        dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
        dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
        dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
        this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Column1,
        this.Column2,
        this.Column3});
        this.dataGridView1.EnableHeadersVisualStyles = false;
        this.dataGridView1.Location = new System.Drawing.Point(49, 62);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(443, 309);
        this.dataGridView1.TabIndex = 0;
        // 
        // Column1
        // 
        this.Column1.HeaderText = "Column1";
        this.Column1.Name = "Column1";
        // 
        // Column2
        // 
        this.Column2.HeaderText = "Column2";
        this.Column2.Name = "Column2";
        // 
        // Column3
        // 
        this.Column3.HeaderText = "Column3";
        this.Column3.Name = "Column3";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 14F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(546, 457);
        this.Controls.Add(this.dataGridView1);
        this.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
        this.Name = "Form1";
        this.Text = "Form1";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.ResumeLayout(false);

    }

Now as you can see, your font setting for Datagridview header did saved. But still, font setting for your form appeared after that, which eventually overrides Datagridview font setting.

My advice is return the Form font setting to default.

Samuel Adam
  • 1,327
  • 4
  • 26
  • 45
  • The problem is that the form font (or any other) setting is only supposed to propagate to the datagrid column header styles if they aren't set (just like with every other winforms control). This is not the case here. – svinja Aug 07 '12 at 09:01
  • There is a difference when you applied font style to datagridview column header with other controls. When you say `this.button1.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));` it will use Font getter setter from Control class. But this `dataGridViewCellStyle1.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));` it's using Font from DataGridViewCellStyle class. – Samuel Adam Aug 07 '12 at 09:30
  • And seeing `System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();` gives me the idea that DataGridViewCellStyle is on namespace System.Windows.Forms which is the same namespace used by every Form there is. Which means their Font style declaration is in the same namespace. – Samuel Adam Aug 07 '12 at 09:35
1

Use this code

dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58
TinhLe
  • 11
  • 1
1

Try this:

DataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Calibri", 9.75F, FontStyle.Bold);       
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
1

This is a bug and still there even in .net 4.6, the problem is that the ColumnHeadersDefaultCellStyle font always overwritten by its Parent font so I figured out a fix for this:

First you need to add your DataGridView to an own Panel, the Panel will work here as a shield and I believe you need to set the Dock property of the DataGridView to Fill.

Second you need to add the following code into ColumnHeadersDefaultCellStyleChanged event.

 If Parent IsNot Nothing Then
       Parent.Font = ColumnHeadersDefaultCellStyle.Font
  End If
Top Systems
  • 951
  • 12
  • 24
  • 1
    Correct answer. Adjusting the parent font (in my case a GroupBox) was the only working solution. Thank you! – PSchn Jun 26 '20 at 11:06
0

I had the same issue today and it seemed that the ColumnHeadersDefaultCellStyle of the DataGridView is overwritten by the font style of the form it belongs to.

As a solution I set the GdiCharSet parameter of the form's font to 0. After that beeing done, the font of the ColumnHeadersDefaultCellStyle won't be overwritten.

I'm on VS 2010 and Window 8.

0

I know this topic is old, however I was having the same issue in VS 2015 with the ColumnDefaultHeadersCellStyle font size always reverting to 10pt (I needed it to be 14pt). I was able to fix this by first changing the font itself, which then allowed me to change the font size.

The font I was originally using was SEGOE UI SEMIBOLD, which I changed to just SEGOE UI and was able to change the size. I haven't looked into why using the semibold version prevented me from changing the size. Further, this method has worked for me with VisualStyles enabled, and EnableHeadersVisualStyles set to true.

If anyone is still having this problem, my suggestion would be try changing to another font.

0

You can try that;

Private Sub DgvListeFt_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DgvListeFt.CellPainting
    Call KolonBaslikDGV(sender, e)
End Sub
Sub KolonBaslikDGV(ByVal S As Object, ByVal E As DataGridViewCellPaintingEventArgs)
    E.PaintBackground(E.CellBounds, True)
    If E.RowIndex = -1 Then
        If E.Value Is Nothing Then
            E.Handled = True
            Return
        End If
        E.Handled = True

        Dim headerFont = New Font("Ariel", 9, FontStyle.Regular)
        Dim myBounds As Rectangle = E.CellBounds
        myBounds.X = E.CellBounds.X + 4
        Dim sf = New StringFormat With {.Alignment = StringAlignment.Near,
                                        .LineAlignment = StringAlignment.Center}
        E.Graphics.DrawString(E.Value.ToString, headerFont, Brushes.MediumVioletRed, myBounds, sf)
        headerFont.Dispose()
        sf.Dispose()
    End If
End Sub