Results 1 to 5 of 5

Thread: [RESOLVED] DataGridView Bold One Row

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    284

    Resolved [RESOLVED] DataGridView Bold One Row

    How can I modify this:

    vb Code:
    1. 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:
    1. Private Sub Content_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Content.CellDoubleClick
    2.         row = e.RowIndex
    3.         If row >= 0 Then
    4.             Content.Font = New Font("Verdana", 8, FontStyle.Bold)
    5.         End If
    6.     End Sub

    Thanks!
    I am still very new to VB.NET, so I have MANY questions

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: DataGridView Bold One Row

    You will have to set that individual row's DefaultCellStyle, like so:

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim BoldRow As New DataGridViewCellStyle With {.Font = New System.Drawing.Font("Verdana", 8.0!, FontStyle.Bold)}
    3.         Me.DataGridView1.Rows(rowindex).DefaultCellStyle = BoldRow
    4.     End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    284

    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:
    1. Content.Font = New Font("Verdana", 8, FontStyle.Regular) ' I was expecting this row to make them all normal font...
    2. Dim BoldRow As New DataGridViewCellStyle With {.Font = New System.Drawing.Font("Verdana", 8.0!, FontStyle.Bold)}
    3. Content.Rows(row).DefaultCellStyle = BoldRow
    I am still very new to VB.NET, so I have MANY questions

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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:
    1. Public Class Form1
    2.     Private BoldedRows As New List(Of DataGridViewRow)
    3.  
    4.     Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
    5.         Me.DataGridView1.Rows(e.RowIndex).DefaultCellStyle = New DataGridViewCellStyle With {.Font = New Drawing.Font("Verdana", 8.0!, FontStyle.Bold)}
    6.         Me.BoldedRows.Add(Me.DataGridView1.Rows(e.RowIndex))
    7.     End Sub
    8.  
    9.     Public Sub ResetBoldedRows()
    10.         For Each BoldRow As DataGridViewRow In Me.BoldedRows
    11.             Me.DataGridView1.Rows(Me.DataGridView1.Rows.IndexOf(BoldRow)).DefaultCellStyle = Me.DataGridView1.RowsDefaultCellStyle
    12.         Next
    13.         Me.BoldedRows.Clear()
    14.     End Sub
    15.  
    16.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    17.         ResetBoldedRows()
    18.     End Sub
    19. End Class

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    284

    Re: DataGridView Bold One Row

    Nice Thanks!
    I am still very new to VB.NET, so I have MANY questions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width