Results 1 to 3 of 3

Thread: [RESOLVED] DataGridView Cell Formatting

  1. #1

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    53

    Resolved [RESOLVED] DataGridView Cell Formatting

    I have a gridview set up, with a datatable I am writing to and then using it as a binding source so I can use the filter on it, as you can see in the code snippet below.

    I am trying to format the first row and first column with bold, but only the column is working, not the row.

    Code:
    Private bsScoreboard As BindingSource = New BindingSource()
    Private dtScoreboard As DataTable = New DataTable()
     
    <Create columns for datatable>
     
    <Progamatically add rows>
     
    bsScoreboard.DataSource = dtScoreboard
    dgvScoreboard.DataSource = bsScoreboard
     
    dgvScoreboard.Rows(0).DefaultCellStyle.Font = New Font(dgvScoreboard.Font, FontStyle.Bold)
    dgvScoreboard.Columns(0).DefaultCellStyle.Font = New Font(dgvScoreboard.Font, FontStyle.Bold)


    I also tried doing specific cells bold such as below, but nothing happened either:

    Code:
    dgvScoreboard.Rows(0).Cells(1).Style.Font = New Font(dgvScoreboard.Font, FontStyle.Bold)

    What am I doing wrong?


    Joe

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: DataGridView Cell Formatting

    I just tested and it worked fine for me and I suspect that it's working fine for you to. Are you filtering after setting those Font properties? When you filter, the existing rows will be removed from the grid and new DataGridViewRow objects created. That means that the row whose DefaultCellStyle you modified is no longer in the grid so you won't see that formatting. The columns don't change so you don't lose the column formatting. You'll need to reapply the formatting each time the rows change. That's why formatting is often done in the CellFormatting event handler, i.e. each cell can take care of its own formatting whenever it needs to, e.g.
    vb.net Code:
    1. Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    2.     If e.ColumnIndex = 0 OrElse e.RowIndex = 0 Then
    3.         e.CellStyle.Font = New Font(Me.DataGridView1.Font, FontStyle.Bold)
    4.     End If
    5. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    53

    Re: DataGridView Cell Formatting

    Oh geez, this is what I get for looking at it for too long and too late into the night.

    I was doing a filter after formatting, but the filter didn't affect the content I was formatting, and the column was working fine. I moved the formatting after the filter and it is working fine now. Thank you!

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