Change background color at run time DataList

I have the following code to chnage the row color according to a db status, but something to seems to be wrong, I dont get error, but the cells dont change the color..

any ideas?
<asp:DataList ID="DataList1" runat="server" BackColor="White" 
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            DataKeyField="Id" DataSourceID="SqlDataSource1" GridLines="Vertical"
          oneditcommand="DataList1_EditCommand" 
            OnUpdateCommand="DataList1_UpdateCommand" 
            OnCancelCommand="DataList1_CancelCommand" style="font-size: x-small" 
            Width="16px" Height="16px" 
             onitemdatabound="DataList1_ItemDataBound"           >




.cs
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item ||
          e.Item.ItemType == ListItemType.AlternatingItem)
        {
            System.Data.DataRowView drv =
                (System.Data.DataRowView)(e.Item.DataItem);
            string status = drv.Row["Status"].ToString();
            if (status == "Available")
                e.Item.BackColor = System.Drawing.Color.LightGreen;
            if (status == "Assigned")
                 e.Item.BackColor = System.Drawing.Color.LightSteelBlue;
             if (status == "BR")
                 e.Item.BackColor = System.Drawing.Color.LightSalmon;
            
        }

       
    }

Open in new window

LVL 9
TonyRebaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TonyRebaAuthor Commented:
Here is my complete datalist
<asp:DataList ID="DataList1" runat="server" BackColor="White" 
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            DataKeyField="Id" DataSourceID="SqlDataSource1" GridLines="Vertical"
          oneditcommand="DataList1_EditCommand" 
            OnUpdateCommand="DataList1_UpdateCommand" 
            OnCancelCommand="DataList1_CancelCommand" style="font-size: x-small" 
            Width="16px" Height="16px" 
             onitemdatabound="DataList1_ItemDataBound"           >
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" >
                  <asp:ListItem Text="Assigned" Value="Assigned">Assigned</asp:ListItem> 
                    <asp:ListItem Text="Available" Value="Available">Available</asp:ListItem>
                    <asp:ListItem Text="BR" Value="BR">BR</asp:ListItem>
                  
                </asp:DropDownList>
            
                <tr>   
                <td>     
                <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="update">Update</asp:LinkButton> 
               <asp:LinkButton ID="lnkCancel" runat="server" CommandName="cancel">Cancel</asp:LinkButton> 
                  </td></tr>
            </EditItemTemplate>
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <FooterTemplate>
                Census
                  <asp:TextBox ID="txt_CensusNT1" runat="server" Width="35px" ></asp:TextBox>
                Comment
                <asp:TextBox ID="txt_CommentNT1" runat="server" Width="70px" 
                    TextMode="MultiLine"></asp:TextBox>
                <asp:Button ID="Button4" runat="server" OnClick="btnApply_Click" 
                    style="font-size: x-small" Text="Save" />
                  <br></br>

            </FooterTemplate>
            <HeaderStyle BackColor="Blue" Font-Bold="True" ForeColor="White" />
            <HeaderTemplate>
                <div class="style3">
                    NT-1
                </div>
            </HeaderTemplate>
            <AlternatingItemStyle CssClass="alternating" BackColor="#DCDCDC" />
            <ItemStyle BackColor="#EEEEEE" ForeColor="Black" />
            <ItemTemplate>
               <asp:Label ID="BedNumberLabel" runat="server" 
                    Text='<%# Eval("BedNumber") %>' /> -<asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>' />
  
               <br /> 
  
  <asp:LinkButton runat="server" ID="LinkButton1" 
                    CommandName="edit" >
                    Edit
                </asp:LinkButton>
                
            </ItemTemplate>

            <SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        </asp:DataList>
  
       
             </td>
             <td class="style19">
 
    <span class="style8">
   
     <asp:DataList ID="DataList2" runat="server" BackColor="White" 
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            DataKeyField="Id" DataSourceID="SqlDataSource2" GridLines="Vertical"
          oneditcommand="DataList2_EditCommand" 
            OnUpdateCommand="DataList2_UpdateCommand" 
            OnCancelCommand="DataList2_CancelCommand" style="font-size: x-small" 
            Width="16px" Height="16px"           >
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList2" runat="server" >
                  <asp:ListItem Text="Assigned" Value="Assigned">Assigned</asp:ListItem> 
                    <asp:ListItem Text="Available" Value="Available">Available</asp:ListItem>
                    <asp:ListItem Text="BR" Value="BR">BR</asp:ListItem>
            </asp:DropDownList>
            
                <tr>   
                <td>     
                <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="update">Update</asp:LinkButton> 
               <asp:LinkButton ID="lnkCancel" runat="server" CommandName="cancel">Cancel</asp:LinkButton> 
                  </td></tr>
            </EditItemTemplate>
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <FooterTemplate>
                Census
                  <asp:TextBox ID="txt_CensusNT2" runat="server" Width="35px" ></asp:TextBox>
                Comment
                <asp:TextBox ID="txt_CommentNT2" runat="server" Width="70px" 
                    TextMode="MultiLine"></asp:TextBox>
                <asp:Button ID="Button4" runat="server" OnClick="btnApply2_Click" 
                    style="font-size: x-small" Text="Save" />
                  <br></br>

            </FooterTemplate>
            <HeaderStyle BackColor="Blue" Font-Bold="True" ForeColor="White" />
            <HeaderTemplate>
                <div class="style3">
                    NT-2
                </div>
            </HeaderTemplate>
            <AlternatingItemStyle CssClass="alternating" BackColor="#DCDCDC" />
            <ItemStyle BackColor="#EEEEEE" ForeColor="Black" />
            <ItemTemplate>
                &nbsp;<asp:Label ID="BedNumberLabel" runat="server" 
                    Text='<%# Eval("BedNumber") %>' />
                -<asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>' />
  
  <asp:LinkButton runat="server" ID="LinkButton1" 
                    CommandName="edit" >
                    Edit
                </asp:LinkButton>
                
            </ItemTemplate>

            <SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        </asp:DataList>
  
       
             </td>
             <td class="style19">
    <span class="style8">
   
     <asp:DataList ID="DataList3" runat="server" BackColor="White" 
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            DataKeyField="Id" DataSourceID="SqlDataSource3" GridLines="Vertical"
          oneditcommand="DataList3_EditCommand" 
            OnUpdateCommand="DataList3_UpdateCommand" 
            OnCancelCommand="DataList3_CancelCommand" style="font-size: x-small" 
            Width="16px" Height="16px"           >
            <EditItemTemplate>
            <asp:DropDownList ID="DropDownList3" runat="server" >
                  <asp:ListItem Text="Assigned" Value="Assigned">Assigned</asp:ListItem> 
                  <asp:ListItem Text="Available" Value="Available">Available</asp:ListItem>
                  <asp:ListItem Text="BR" Value="BR">BR</asp:ListItem>
            </asp:DropDownList>
            
                <tr>   
                <td>     
                <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="update">Update</asp:LinkButton> 
               <asp:LinkButton ID="lnkCancel" runat="server" CommandName="cancel">Cancel</asp:LinkButton> 
                  </td></tr>
            </EditItemTemplate>
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <FooterTemplate>
                Census
                  <asp:TextBox ID="txt_CensusNT3" runat="server" Width="35px" ></asp:TextBox>
                Comment
                <asp:TextBox ID="txt_CommentNT3" runat="server" Width="70px" 
                    TextMode="MultiLine"></asp:TextBox>
                <asp:Button ID="Button4" runat="server" OnClick="btnApply3_Click" 
                    style="font-size: x-small" Text="Save" />
                  <br></br>

            </FooterTemplate>
            <HeaderStyle BackColor="Blue" Font-Bold="True" ForeColor="White" />
            <HeaderTemplate>
                <div class="style3">
                    NT-3</div>
            </HeaderTemplate>
            <AlternatingItemStyle CssClass="alternating" BackColor="#DCDCDC" />
            <ItemStyle BackColor="#EEEEEE" ForeColor="Black" />
            <ItemTemplate>
                &nbsp;<asp:Label ID="BedNumberLabel" runat="server" 
                    Text='<%# Eval("BedNumber") %>' />
                -<asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("Status") %>' />
  
  <asp:LinkButton runat="server" ID="LinkButton1" 
                    CommandName="edit" >
                    Edit
                </asp:LinkButton>
                
            </ItemTemplate>

            <SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        </asp:DataList>

Open in new window

0
MlandaTCommented:
it all  looks like it should work. however, just as a test, let's add a "default" background colour...

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.Item ||
          e.Item.ItemType == ListItemType.AlternatingItem)
        {
            System.Data.DataRowView drv =
                (System.Data.DataRowView)(e.Item.DataItem);
            string status = drv.Row["Status"].ToString();
            if (status == "Available")
                e.Item.BackColor = System.Drawing.Color.LightGreen;
            else if (status == "Assigned")
                 e.Item.BackColor = System.Drawing.Color.LightSteelBlue;
            else if (status == "BR")
                 e.Item.BackColor = System.Drawing.Color.LightSalmon;
            else 
                 e.Item.BackColor = System.Drawing.Color.Pink;
            
        }

}

Open in new window


am suspecting that your conditions are never evaluating to true
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.