views:

33

answers:

1

I'm trying to change the backcolor of a .Net GridView when a certain condition is met. This should be fairly straight forward, but the color change isn't happening. Checking the rendered HTML shows absolutely nothing different in the effected rows even though the text change on the link is happening as expected.

There is a default theme set for all gridviews and I haven't worked with that in the past. Is that preventing me from dynamically changing the color of a row or am I missing something else?

I'm using .Net 3.5 and have the following in my code behind:

Protected Sub gvPrevious_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvPrevious.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If currentCorrespondence.IsRetired Then //custom object, returning as expected
            Dim link As LinkButton = DirectCast(e.Row.Controls(4).Controls(2), LinkButton) 
            link.Text = "Reinstate" //default is "retire" - GV renders this as expected
            e.Row.BackColor = Drawing.Color.IndianRed //might as well not be here 
        End If
    End If
End Sub

From the above code, when I go into the browser and view source, I would expect to see <tr style="background-color: #CD5C5C;"> for effected rows. Instead, I see <tr> and <tr class="AlternateRowStyle"> (where appropriate). I see absolutely no effects from the BackColor property change.

I have also attempted to use e.Row.CssClass with the same result.

+1  A: 

try something like this:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    Select Case e.Row.RowState
        Case DataControlRowState.Normal
            CType(e.Row, GridViewRow).Style.Add("background", "#CD5C5C")
    End Select
End Sub
Tom Halladay