how to determine if fontstyle is bold or regular in a datagridview

G

Guest

I have code to bold text in a datagridviewcell:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim cs As DataGridViewCellStyle, fnt As Font
For Each c As DataGridViewCell In dgrv1.SelectedCells
cs = c.Style
Console.WriteLine(c.Style.Font)
fnt = dgrv1.Font
Console.WriteLine(fnt.Name)
cs.Font = New Font(fnt.Name, fnt.Size, FontStyle.Bold)
Next
End Sub

This works fine for bolding the text in the datagridviewcell, but how to
unbold this text?

I want to be able to click the same button to unbold the text in selected
cells in the datagridview if the text is bold. But if the text is not bold
then make the text bold. Like a toggle. I can only set fnt to the
datagridview.Font property. I tried setting fnt to the
datagridviewcell.style.font property but that did not set anything to fnt.

I tried the following to determine if Fontstyle was regular but it errored out

Dim cs As DataGridViewCellStyle, fnt As Font
For Each c As DataGridViewCell In dgrv1.SelectedCells
cs = c.Style
fnt = dgrv1.Font
If cs.Font.Style = FontStyle.Regular Then '--error here
cs.Font = New Font(fnt.Name, fnt.Size, FontStyle.Bold)
End If
Next

The error message for the following line of code

If cs.Font.Style = FontStyle.Regular

said that the object was not set - referring to the Font object -

but this was the only syntax that contained FontStyle expression in the
intellisense. Anybody have an idea how to determine if the fontstyle,
bold/regular, for a datagridviewcell?

Thanks,
Rich
 
C

ClayB

Instead of

If cs.Font.Style = FontStyle.Regular Then '--error here
cs.Font = New Font(fnt.Name, fnt.Size,
FontStyle.Bold)
End If

you might try this code:

If cs.Font is Nothing OrElse cs.Font.Style =
FontStyle.Regular Then
cs.Font = New Font(fnt.Name, fnt.Size,
FontStyle.Bold)
End If


================
Clay Burch
Synfusion, Inc.
 
G

Guest

Rocknroll ! that did the trick! Thanks (JIT because I just got tasked with
something way less fun)

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top