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
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