[RESOLVED] DataGridView Bold One Row
How can I modify this:
vb Code:
Content.Font = New Font("Verdana", 8, FontStyle.Bold)
So that in my datagridview it only makes the selected row bold when it is double clicked?
Whole Thing:
vb Code:
Private Sub Content_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Content.CellDoubleClick
row = e.RowIndex
If row >= 0 Then
Content.Font = New Font("Verdana", 8, FontStyle.Bold)
End If
End Sub
Thanks!
Re: DataGridView Bold One Row
You will have to set that individual row's DefaultCellStyle, like so:
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim BoldRow As New DataGridViewCellStyle With {.Font = New System.Drawing.Font("Verdana", 8.0!, FontStyle.Bold)}
Me.DataGridView1.Rows(rowindex).DefaultCellStyle = BoldRow
End Sub
Re: DataGridView Bold One Row
Kool that works!
Now when I change rows by double clicking the previously bolded row stays bold, why is that?
vb.net Code:
Content.Font = New Font("Verdana", 8, FontStyle.Regular) ' I was expecting this row to make them all normal font...
Dim BoldRow As New DataGridViewCellStyle With {.Font = New System.Drawing.Font("Verdana", 8.0!, FontStyle.Bold)}
Content.Rows(row).DefaultCellStyle = BoldRow
Re: DataGridView Bold One Row
It's because that row is no longer pointing to the DataGridView.RowsDefaultCellStyle anymore. You have given it a new custom CellStyle. So, you will have to reset the rows where you have given it a custom font to the DataGridView.RowsDefaultCellStyle again (or a new CellStyle, whichever suits your needs).
This is what I would do. Everytime you bold a row, add it to a class level variable called bolded rows. Then when you want to unbold those rows, you loop through that list and change their CellStyle. Like so:
vb.net Code:
Public Class Form1
Private BoldedRows As New List(Of DataGridViewRow)
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Me.DataGridView1.Rows(e.RowIndex).DefaultCellStyle = New DataGridViewCellStyle With {.Font = New Drawing.Font("Verdana", 8.0!, FontStyle.Bold)}
Me.BoldedRows.Add(Me.DataGridView1.Rows(e.RowIndex))
End Sub
Public Sub ResetBoldedRows()
For Each BoldRow As DataGridViewRow In Me.BoldedRows
Me.DataGridView1.Rows(Me.DataGridView1.Rows.IndexOf(BoldRow)).DefaultCellStyle = Me.DataGridView1.RowsDefaultCellStyle
Next
Me.BoldedRows.Clear()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ResetBoldedRows()
End Sub
End Class
Re: DataGridView Bold One Row