Search code examples
vb.netdatagridviewformattingcell

DataGridView only refreshing styles upon cell click


I have a DGV in VB.NET and VS2012. I am attempting to change the cell formatting of various cells dynamically. The following is my code:

Private Sub gridFinancial_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles gridFinancial.CellFormatting
    Try
        For Each row As chgltrDataSet.gridsourceRow In frmFinBatchChrg.ChgltrDataSet.gridsource.Rows
            If gridFinancial.CurrentRow.Cells("CompBool").Value = True Then
                Me.gridFinancial.CurrentRow.Cells(0).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(1).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(2).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(3).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(4).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(5).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(6).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(7).Style.BackColor = Color.Yellow
                Me.gridFinancial.CurrentRow.Cells(8).Style.BackColor = Color.Yellow

                Me.gridFinancial.CurrentRow.Cells(0).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(1).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(2).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(3).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(4).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(5).ReadOnly = True
                Me.gridFinancial.CurrentRow.Cells(6).ReadOnly = True
                Me.gridFinancial.Update()
                Me.gridFinancial.Refresh()
    End if

    Catch ex As Exception

    End Try


End Sub

I have read this: http://msdn.microsoft.com/en-us/library/1yef90x0.aspx and maybe I'm missing something, but right now, with that code applied, my DataGridView will only reflect that code if I click one of the affected cells after the DataGridView has been painted. In other words, after the DataGridView has loaded, the cells will only be yellow after I click them (then all of the cells within that row that are supposed to be yellow, appear yellow). Why is this? I'm not sure what I'm doing wrong.

And as a side question, this cell formatting event fires at least 40-50 times before my DGV has even been drawn, and it's only a 6 row DataSource. Isn't there a better event trigger for this? I'm sure my code could be better, but that just seems highly inefficient.

The readonly properties in the above code work fine, so I know the event is triggering correctly.


Solution

  • Instead of setting the cell style like you are you can access the style through the DataGridViewCellFormattingEventArgs.

    So something like this:

    e.CellStyle.BackColor = Color.Yellow
    

    Another thing to consider is that it is often better to attach your CellFormatting handler from within the DataBindingComplete event.

    Private Sub DataGridView1_DataBindingComplete(sender As System.Object, e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
        AddHandler DataGridView1.CellFormatting, AddressOf Me.DataGridView1_CellFormatting
    End Sub
    

    This addresses some odd behaviour when the visual portion of the DataGridView is rendered by making sure that everything is done before your later events are attached.


    This tip with the DataBindingComplete event should at least reduce the needless calls to CellFormatting - cell formatting fires every time a cell is updated. Once you are all up and running this is only once a user leaves a cell.