salvationishere salvationishere - 1 year ago 86 Question

How to change the background color conditionally according to row values

I am developing a report which outputs data in a SQL Table via an ASPX page, using VB. How do I code this so that all rows containing a "1" for one of the columns, shows up as red? Here is what I have so far. This doesn't cause any errors, but it doesn't show up as red either.

I don't know if this is a problem, but the ExceedsLimit and SixInARow variables are INTs in my stored procs, but then I am declaring them as strings in the code below.

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Dim ExceedsLimit As String
Dim SixInARow As String
Dim i As Int16

SixInARow = e.Item.Cells(6).Text
ExceedsLimit = e.Item.Cells(7).Text

If ExceedsLimit = "1" Or SixInARow = "1" Then
For i = 0 To 6
e.Item.Cells(i).BackColor = System.Drawing.Color.Red
End If

End If
End Sub

And the HTML portion:

<ASP:DATAGRID id="dgTable" runat="server" AUTOGENERATECOLUMNS="true" ShowHeader="true" OnItemDataBound="DataGrid1_ItemDataBound" >
<AlternatingItemStyle BackColor = "#eeeeee" />
<HEADERSTYLE BackColor = "#336699" ForeColor = "#ffffff" Font-Bold = "true" />

Answer Source

You need to set the background color to the Item.Style

Try this:

If ExceedsLimit = "1" Or SixInARow = "1" Then                 
  e.Item.BackColor = System.Drawing.Color.Red                 
End If 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download