Bold contents of column in datagridview control vb.net

Hi all.

I want to bold the cells in column 5 of my datagridview control but I'm not having any success.

I've tried putting the following code in the Load and Layout events for the datagridview control:

myDataGridView.Columns(5).DefaultCellStyle.Font = New Font(Control.DefaultFont, FontStyle.Bold)

Open in new window


But no success.

The datagridview control is bound.

Here's what my form Load event looks like:

Private Sub myForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Me.myFormTableAdapter.Fill(Me.MAS_DataDataSet.myTable)

        myDataGridView.DefaultCellStyle.WrapMode = DataGridViewTriState.True     'Word wrap 
        myDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells  'Adjust the height of the rows
        myDataGridView.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter    'Center in the middle cell contents
        myDataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter   'Center in the middle the column headers
        myDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige

       myDataGridView.Columns(5).DefaultCellStyle.Font = New Font(Control.DefaultFont, FontStyle.Bold)

        txtRowCount.Text = myDataGridView.RowCount

    End Sub

Open in new window


Any ideas? Thank you in advance.
printmediaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
if "column 5" means the fifth column, because columns are 0-based, you should use index 4.
0
printmediaAuthor Commented:
It's really column 6 which is why I use Columns(5).
0
Éric MoreauSenior .Net ConsultantCommented:
Where do you set your datagrid's DataSource property?

You probably have another setting overwriting this one elsewhere.

Have you tried setting the Font in the DataSourceChanged event handler?

    Private Sub dataGridView1_DataSourceChanged(sender As Object, e As EventArgs) Handles myDataGridView.DataSourceChanged
        myDataGridView.Columns(5).DefaultCellStyle.Font = New Font(Control.DefaultFont, FontStyle.Bold)
    End Sub

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

printmediaAuthor Commented:
The datasource comes from the dataset listed in my Load event above. I tried your suggestion and it's still not bolding the column cells.
0
Éric MoreauSenior .Net ConsultantCommented:
but how is the dataset pushed to the grid? I don't see you setting the DataSource anywhere?
0
Éric MoreauSenior .Net ConsultantCommented:
I have done a test on my side and the code is working. I set the DataSource property to a DataTable (not a DataSet)
0
printmediaAuthor Commented:
Via dataset.xsd it's not loaded through code but rather through the Visual Studio new datasource wizard.
0
Éric MoreauSenior .Net ConsultantCommented:
I never use databindings!

Give a try to the CellFormatting event instead:
    Private Sub dataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles MyDataGridView.CellFormatting
        if e.ColumnIndex=5 Then
            e.CellStyle.Font = New Font(Control.DefaultFont, FontStyle.Bold)
        End If
    End Sub

Open in new window

0
printmediaAuthor Commented:
Tried that and it didn't work. The end user has to fill in 2 of the last columns in the datagridview control and then when they're done they click a button and the data in the datagridview gets inserted into a SQL table, I didn't know if Visual Studio allowed a datagridview control to be updated if it wasn't through databinding.
0
Éric MoreauSenior .Net ConsultantCommented:
I will let others answer then! I am allergic to DataBindings done through the designer.

I my test app (not using data bindings), I have everything to work perfectly.
0
printmediaAuthor Commented:
Ok. Thank you for helping.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
use the defaultCellStyle instead.
e.DefaultCellStyle.Font

or  try

e.Row.Cells(5).Style.Font.Bold = true

Open in new window

0
printmediaAuthor Commented:
Yes I tried DefaultCellStyle see code examples above.

What event would I put your code:

e.Row.Cells(5).Style.Font.Bold = true

Open in new window

0
Kyle AbrahamsSenior .Net DeveloperCommented:
Sorry . . . this will work for you:
Private Sub dataGridView1_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs)
    If e.ColumnIndex=5 Then dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.Font = New Font(Control.DefaultFont, FontStyle.Bold)
End Sub

Open in new window



or if you're going for the whole column regardless apply this on the form load:


            dataGridView1.Columns(5).DefaultCellStyle.Font = new Font(Control.DefaultFont, FontStyle.Bold)

Open in new window


Note that you should set that after you set the datasource otherwise you may get an out of bounds exception (if the datagridview columns aren't pre-made).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
printmediaAuthor Commented:
That was it! I tweaked it a little:

Dim cell As DataGridViewCell = dataGridView1.Rows(e.RowIndex).Cells("ItemNumber")
        cell.Style.Font = New Font(Control.DefaultFont, FontStyle.Bold)

Open in new window


Thank you to both you and Eric.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.