Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Bold Font DataGridView Column
 

Bold Font DataGridView Column

Greetings All,

I am trying to set the font of certain columns in a bound DataGridView to "Bold ". Searched and found two snippets:
Dim style As DataGridViewCellStyle = New DataGridViewCellStyle
 style.Font = New Font(frmMain.dgvPriceView.Font, FontStyle.Bold)

frmMain.dgvPriceView.Columns(0).DefaultCellStyle = style
And:
frmMain.dgvPriceView.Columns(1).DefaultCellStyle.Font = New Font(frmMain.dgvPriceView.Font, FontStyle.Bold)

This does not work for my columns, but if "Columns" is changed to "Rows" this will bolden all cells in a row?

I'm convinced that I'm overlooking some simple setting somewhere, but am at my wit's end figuring out where.

Help, Please!

Thanks in advance,

Mike
mwalsh62  Friday, September 04, 2009 10:39 PM
Hi Mike

I had a play with your DGV properties.
With the font specification on the AlternatingRowsDefaultCellStyle and the RowsDefaultCellStyle properties the column style font is not applied. It would seem that row styles have priority over column styles.

Seehttp://msdn.microsoft.com/en-us/library/1yef90x0.aspx.

Removing the font setting for those two properties give me bold text in my specified column.
  • Edited byWole Ogunremi Sunday, September 06, 2009 11:10 AMcorrected grammar
  • Marked As Answer bymwalsh62 Sunday, September 06, 2009 9:28 PM
  •  
Wole Ogunremi  Sunday, September 06, 2009 10:28 AM
Hi Mike

Looks like you're close to it. The code below which is virtually the same as yours works.

System.Drawing.Font currentFont = dgv.Font;
DataGridViewCellStyle newCellStyle = new DataGridViewCellStyle();
newCellStyle.Font = new System.Drawing.Font(currentFont, FontStyle.Bold);
this.dgv.Columns[1].DefaultCellStyle = newCellStyle;

This matches your code where you set the default cell style of column 0 - isn't column 0 the row headers column?
Good luck!
Wole Ogunremi  Friday, September 04, 2009 11:31 PM
Hi again,

Thanks for your time and thought. I converted your example to .Net (with no errors) and got the same result; no bold in "Columns", but changing to "Rows" boldened specified row.

Dim currentFont As System.Drawing.Font = frmMain.dgvPriceView.Font
Dim newCellStyle As New DataGridViewCellStyle()

newCellStyle.Font = New System.Drawing.Font(currentFont, FontStyle.Bold)
frmMain.dgvPriceView.Columns(0).DefaultCellStyle = newCellStyle
Any ideas?

Mike

mwalsh62  Friday, September 04, 2009 11:45 PM
Do you have the option of defining the particular column's display properties at design time? If yes I would try setting it with via the Properties Window. You'll see a small button with the ellipsis "..." which when click brings up the Columns Collection. You can modify the font properties for any selected column therein.

Other than that, the code you've pasted above code works. You might have to paste more code to enable reproduction.
Wole Ogunremi  Saturday, September 05, 2009 12:18 AM
Hi,

No, DGV is bound to DataTable through code, no designer or wizard, columns are auto-generated thus no columns listed in collection.

Are there any settings in the DGV itself which would prevent manipulation of column font?

Thanks again,

Mike
mwalsh62  Saturday, September 05, 2009 10:37 AM
I just noticed your question about row headers a couple of posts ago. Row headers are not visible, and I have tried the code on multiple columns, no effect.
mwalsh62  Saturday, September 05, 2009 10:46 AM
Should still work even with auto-generated columns...

Can you post the initialization code for your DGV [ie DGV related code inInitializeComponent()], and any other code that manipulates it? I would like to see if I can reproduce it. Leave out the data access/population bits (not unless you're doing something out of the norm...)

It's probably also worth confirming which version of DGV you're using: I tried it with the v2.0.0.0

Thanks
Wole Ogunremi  Saturday, September 05, 2009 11:34 AM
Hi,

Here is the InitializeComponent:

Me.dgvPriceView.AllowUserToAddRows = False
        Me.dgvPriceView.AllowUserToDeleteRows = False
        Me.dgvPriceView.AllowUserToResizeColumns = False
        Me.dgvPriceView.AllowUserToResizeRows = False
        DataGridViewCellStyle10.BackColor = System.Drawing.Color.WhiteSmoke
        DataGridViewCellStyle10.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        DataGridViewCellStyle10.ForeColor = System.Drawing.Color.Black
        DataGridViewCellStyle10.SelectionBackColor = System.Drawing.Color.WhiteSmoke
        DataGridViewCellStyle10.SelectionForeColor = System.Drawing.SystemColors.HighlightText
        DataGridViewCellStyle10.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
        Me.dgvPriceView.AlternatingRowsDefaultCellStyle = DataGridViewCellStyle10
        Me.dgvPriceView.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.dgvPriceView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill
        Me.dgvPriceView.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
        Me.dgvPriceView.BackgroundColor = System.Drawing.Color.White
        DataGridViewCellStyle11.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft
        DataGridViewCellStyle11.BackColor = System.Drawing.SystemColors.Control
        DataGridViewCellStyle11.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        DataGridViewCellStyle11.ForeColor = System.Drawing.SystemColors.WindowText
        DataGridViewCellStyle11.SelectionBackColor = System.Drawing.SystemColors.Highlight
        DataGridViewCellStyle11.SelectionForeColor = System.Drawing.SystemColors.HighlightText
        DataGridViewCellStyle11.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
        Me.dgvPriceView.ColumnHeadersDefaultCellStyle = DataGridViewCellStyle11
        Me.dgvPriceView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing
        DataGridViewCellStyle12.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleRight
        DataGridViewCellStyle12.BackColor = System.Drawing.Color.White
        DataGridViewCellStyle12.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        DataGridViewCellStyle12.ForeColor = System.Drawing.Color.Black
        DataGridViewCellStyle12.SelectionBackColor = System.Drawing.Color.White
        DataGridViewCellStyle12.SelectionForeColor = System.Drawing.Color.Black
        DataGridViewCellStyle12.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
        Me.dgvPriceView.DefaultCellStyle = DataGridViewCellStyle12
        Me.dgvPriceView.EditMode = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically
        Me.dgvPriceView.EnableHeadersVisualStyles = False
        Me.dgvPriceView.GridColor = System.Drawing.Color.LightGray
        Me.dgvPriceView.Location = New System.Drawing.Point(6, 71)
        Me.dgvPriceView.Name = "dgvPriceView"
        Me.dgvPriceView.ReadOnly = True
        Me.dgvPriceView.RowHeadersVisible = False
        Me.dgvPriceView.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders
        DataGridViewCellStyle13.BackColor = System.Drawing.Color.White
        DataGridViewCellStyle13.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        DataGridViewCellStyle13.ForeColor = System.Drawing.Color.Black
        DataGridViewCellStyle13.SelectionBackColor = System.Drawing.Color.White
        DataGridViewCellStyle13.SelectionForeColor = System.Drawing.Color.Black
        DataGridViewCellStyle13.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
        Me.dgvPriceView.RowsDefaultCellStyle = DataGridViewCellStyle13
        Me.dgvPriceView.RowTemplate.Resizable = System.Windows.Forms.DataGridViewTriState.[True]
        Me.dgvPriceView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
        Me.dgvPriceView.Size = New System.Drawing.Size(748, 635)
        Me.dgvPriceView.TabIndex = 17
In the method itself is:

frmMain.dgvPriceView.Columns(3).HeaderText = "UOM"
                frmMain.dgvPriceView.Columns(7).HeaderText = "UOM"
                frmMain.dgvPriceView.Columns(12).HeaderText = "UOM"
                frmMain.dgvPriceView.Columns(1).HeaderText = "Pack"
                frmMain.dgvPriceView.Columns(5).HeaderText = "Pack"
                frmMain.dgvPriceView.Columns(10).HeaderText = "Pack"
                frmMain.dgvPriceView.Columns(2).HeaderText = "Size"
                frmMain.dgvPriceView.Columns(6).HeaderText = "Size"
                frmMain.dgvPriceView.Columns(11).HeaderText = "Size"
                frmMain.dgvPriceView.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
                frmMain.dgvPriceView.Columns(0).Width = 100
                frmMain.dgvPriceView.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                frmMain.dgvPriceView.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                frmMain.dgvPriceView.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                frmMain.dgvPriceView.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                frmMain.dgvPriceView.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                frmMain.dgvPriceView.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                frmMain.dgvPriceView.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                frmMain.dgvPriceView.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                frmMain.dgvPriceView.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                frmMain.dgvPriceView.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                frmMain.dgvPriceView.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                frmMain.dgvPriceView.Columns(12).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                frmMain.dgvPriceView.Columns(13).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

                frmMain.dgvPriceView.Columns(4).DividerWidth = 2.5
                frmMain.dgvPriceView.Columns(9).DividerWidth = 2.5

                For Each dgvCol As DataGridViewColumn In frmMain.dgvPriceView.Columns
                    dgvCol.SortMode = DataGridViewColumnSortMode.NotSortable
                Next

I believe that is it.

Mike
mwalsh62  Saturday, September 05, 2009 7:44 PM
Hi Mike

I had a play with your DGV properties.
With the font specification on the AlternatingRowsDefaultCellStyle and the RowsDefaultCellStyle properties the column style font is not applied. It would seem that row styles have priority over column styles.

Seehttp://msdn.microsoft.com/en-us/library/1yef90x0.aspx.

Removing the font setting for those two properties give me bold text in my specified column.
  • Edited byWole Ogunremi Sunday, September 06, 2009 11:10 AMcorrected grammar
  • Marked As Answer bymwalsh62 Sunday, September 06, 2009 9:28 PM
  •  
Wole Ogunremi  Sunday, September 06, 2009 10:28 AM
Hi Wole,

Thank you very much! I knew it had to be something staring me straight in the face, a not "seeing the forest for the trees" kind of thing.

You have given me the new thing I've learned today.

Thanks again,

Mike
mwalsh62  Sunday, September 06, 2009 9:28 PM

You can use google to search for other answers

Custom Search

More Threads

• please please help me in c# datagrid windows application
• DataRow Values Not Updating In Loop
• addingnew
• Freezing DataGridView Column Removes Horizontal Scrollbar
• Setup for .Net windows application
• DataGridView / DataTable Foreign key replacements
• Problem with custom ComboBox & CurrencyManager
• how do I update db using dataadapter from temporary dataset
• How to Stop Focus ReadOnly Columns
• Datagridview and Excel question