Click here to Skip to main content
14,830,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a listview and I want to change the row background color based on a cells value.

I am able to modify the forecolor properties of each item but I am having no luck with the row.

Private Sub ListViewTraining_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListViewTraining.ItemDataBound
     Dim list As ListView = TryCast(sender, ListView)
     If e.Item.ItemType = ListViewItemType.DataItem Then
         Dim FormatLabelformating As Label = DirectCast(e.Item.FindControl("FormatLabel"), Label)
         Dim ItemNumberLabelformating As Label = DirectCast(e.Item.FindControl("ItemNumberLabel"), Label)
         Dim TrainingLabelformating As Label = DirectCast(e.Item.FindControl("TrainingLabel"), Label)
         Dim StartDateLabelformating As Label = DirectCast(e.Item.FindControl("StartDateLabel"), Label)
         Dim StopDateLabelformating As Label = DirectCast(e.Item.FindControl("StopDateLabel"), Label)
         Dim TrainedByLabelformating As Label = DirectCast(e.Item.FindControl("TrainedByLabel"), Label)
         Dim UpdateLabelformating As Label = DirectCast(e.Item.FindControl("UpdateLabel"), Label)
         Dim UpdatedByLabelformating As Label = DirectCast(e.Item.FindControl("UpdatedByLabel"), Label)
         'Dim nRow As System.Web.UI.WebControls.TableRow = DirectCast(e.Item.FindControl("ItemRow"), System.Web.UI.WebControls.TableRow) tried this but I get can't convert to System.Web.UI. Webcontrols
         If Not IsNothing(FormatLabelformating) Then
             If FormatLabelformating.Text = "h" Then

                 ListViewTraining.Items(e.Item).BackColor = Color.Aqua

                 FormatLabelformating.ForeColor = Color.DarkBlue
                 FormatLabelformating.Font.Bold = True
                 ItemNumberLabelformating.ForeColor = Color.DarkBlue
                 ItemNumberLabelformating.Font.Bold = True
                 TrainingLabelformating.ForeColor = Color.DarkBlue
                 TrainingLabelformating.Font.Bold = True
                 StartDateLabelformating.ForeColor = Color.DarkBlue
                 StartDateLabelformating.Font.Bold = True
                 StopDateLabelformating.ForeColor = Color.DarkBlue
                 StopDateLabelformating.Font.Bold = True
                 TrainedByLabelformating.ForeColor = Color.DarkBlue
                 TrainedByLabelformating.Font.Bold = True
                 UpdateLabelformating.ForeColor = Color.DarkBlue
                 UpdateLabelformating.Font.Bold = True
                 UpdatedByLabelformating.ForeColor = Color.DarkBlue
                 UpdatedByLabelformating.Font.Bold = True
                 FormatLabelformating.ForeColor = Color.Black
                 FormatLabelformating.Font.Bold = False
                 ItemNumberLabelformating.ForeColor = Color.Black
                 ItemNumberLabelformating.Font.Bold = False
                 TrainingLabelformating.ForeColor = Color.Black
                 TrainingLabelformating.Font.Bold = False
                 StartDateLabelformating.ForeColor = Color.Black
                 StartDateLabelformating.Font.Bold = False
                 StopDateLabelformating.ForeColor = Color.Black
                 StopDateLabelformating.Font.Bold = False
                 TrainedByLabelformating.ForeColor = Color.Black
                 TrainedByLabelformating.Font.Bold = False
                 UpdateLabelformating.ForeColor = Color.Black
                 UpdateLabelformating.Font.Bold = False
                 UpdatedByLabelformating.ForeColor = Color.Black
                 UpdatedByLabelformating.Font.Bold = False

             End If
         End If
     End If
 End Sub

    <tr id="ItemRow" runat="server" style="background-color:#FFFFFF;color: #000000; ">
        <td style=" width: 50px">
            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete"  OnClientClick="return confirm('Are you certain you want to delete this item?');"
                Text="Delete" />
            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            <asp:Label ID="ItemNumberLabel" runat="server" Width="55px"
                Text='<%# Eval("ItemNumber") %>' />
            <asp:Label ID="TrainingLabel" runat="server" Width="255px"
                Text='<%# Eval("Training") %>' />
            <asp:Label ID="StartDateLabel" runat="server" Width="100px"
                Text='<%# Eval("StartDate", "{0:dd MMM yyyy}") %>' />
            <asp:Label ID="StopDateLabel" runat="server" Width="100px"
                Text='<%# Eval("StopDate", "{0:dd MMM yyyy}") %>' />
            <asp:Label ID="TrainedByLabel" runat="server" Width="155px" Text='<%# Eval("TrainedBy") %>' />
            <asp:Label ID="FormatLabel" runat="server" Width="55px" Text='<%# Eval("Format") %>' />
            <asp:Label ID="UpdateLabel" runat="server" Text='<%# Eval("Update", "{0:dd MMM yyyy HHmm}") %>' />
            <asp:Label ID="UpdatedByLabel" runat="server" Text='<%# Eval("UpdatedBy") %>' />

Any Ideas?
Updated 11-Feb-12 7:10am

Add this code on ListViewTraining_ItemDataBound to change <TR> backcolor :
If e.Item.ItemType = ListViewItemType.DataItem Then
    Dim dataitem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
    Dim tr As System.Web.UI.HtmlControls.HtmlTableRow
    tr = DirectCast(dataitem.FindControl("ItemRow"), System.Web.UI.HtmlControls.HtmlTableRow)
    tr.BgColor = "#FF0000"
End If
Andy Morris 7-Feb-12 12:30pm
I got it to work but only if the background color is not set in the style from the ASP page.

O-Ghareeb 7-Feb-12 12:36pm
You are welcome.
You only set the back color in one instance, does it not work ? I would be inclined to look closely at the item in the debugger, I have a feeling you have to access it's parent to access the tr element for the entire row.
Andy Morris 7-Feb-12 12:28pm
1, reference problem, I think I have that solved:
Dim row = DirectCast(e.Item.FindControl("Itemrow"), HtmlTableRow)

2, If I set the bgColor to #808080 (Grey) nothing changes.
row.BgColor = "#808080"

I have step through the code to ensure it is firing in the databound event.
Christian Graus 7-Feb-12 12:29pm
What happens if you view the HTML ? Is the BG color set anywhere at all ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900