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?
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.