Change ASP.Net DataList Cell Background Color based on condition using C# and VB.Net

Last Reply 7 months ago By pandeyism

Posted 7 months ago

i have datalist in which one column latedays has digit.

now i want when digits greater than 0 then lateday cell get red. 

Kindly guide thanks. 

here is my c# code

        DataTable dt = new DataTable();
        dt.Clear();
        dt.Columns.Add("TDID");
        dt.Columns.Add("TDDesc");
        dt.Columns.Add("taskdate");
        dt.Columns.Add("enddate");
        dt.Columns.Add("TDstatus");
        dt.Columns.Add("latedays");
        dt.Columns.Add("TDTYPE");

        SqlCommand cmd1 = con.CreateCommand();
        cmd1.CommandType = CommandType.Text;
        cmd1.CommandText = "select TODOLIST.TDID, TODOLIST.TDDesc,Convert(varchar(12),TODOLIST.taskdate,101) as taskdate ,TODOLIST.enddate,TODOLIST.TDstatus,TODOLIST.TDTYPE " +
        " from TODOLIST inner join employee on TODOLIST.empid=employee.empid  where TDstatus ='Open'and employee.EmpID= '" + Session["EmpID"].ToString() + "'";
        cmd1.ExecuteNonQuery();
        DataTable dt1 = new DataTable();
        SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
        da1.Fill(dt1);
        foreach (DataRow dr1 in dt1.Rows)
        {
            DataRow dr = dt.NewRow();
            dr["TDID"] = dr1["TDID"].ToString();
            dr["TDDesc"] = dr1["TDDesc"].ToString();
            dr["taskdate"] = dr1["taskdate"].ToString();
            dr["enddate"] = dr1["enddate"].ToString();
            dr["TDstatus"] = dr1["TDstatus"].ToString();
            dr["TDTYPE"] = dr1["TDTYPE"].ToString();
            DateTime d1 = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd"));
            DateTime d2 = Convert.ToDateTime(dr1["enddate"].ToString());
            if (d1 > d2)
            {
                TimeSpan t = d1 - d2;
                double noofday = t.TotalDays;
                dr["latedays"] = noofday.ToString();
            }
            else
            {
                dr["latedays"] = "0";
            }
            dt.Rows.Add(dr);
        }
        d3.DataSource = dt;
        d3.DataBind();
    }

 

Posted 7 months ago

Hi akhter,

Refer below sample.

HTML

<asp:DataList ID="dlCustomers" runat="server" RepeatLayout="Table" OnItemDataBound="dlCustomers_OnItemDataBound">
    <HeaderTemplate>
        <table class="table">
            <tr>
                <td>
                    TDID
                </td>
                <td>
                    TDDesc
                </td>
                <td>
                    taskdate
                </td>
                <td>
                    enddate
                </td>
                <td>
                    TDstatus
                </td>
                <td>
                    latedays
                </td>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%# Eval("TDID")%>
            </td>
            <td>
                <%# Eval("TDDesc")%>
            </td>
            <td>
                <%# Eval("taskdate")%>
            </td>
            <td>
                <%# Eval("enddate")%>
            </td>
            <td>
                <%# Eval("TDstatus")%>
            </td>
            <td runat="server" id="tbl">
                <asp:Label ID="lblLateDays" Text='<%# Eval("latedays")%>' runat="server" />
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:DataList>

Namespaces

C#

using System.Data;
using System.Web.UI.HtmlControls;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("TDID");
        dt.Columns.Add("TDDesc");
        dt.Columns.Add("taskdate");
        dt.Columns.Add("enddate");
        dt.Columns.Add("TDstatus");
        dt.Columns.Add("latedays");
        dt.Rows.Add(1, "hey", "01/02/2019", "04/05/2019", "Pending", "1");
        dt.Rows.Add(2, "Hi", "05/03/2019", "08/06/2019", "USA", "0");
        this.dlCustomers.DataSource = dt;
        this.dlCustomers.DataBind();
    }
}

protected void dlCustomers_OnItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataListItem item = e.Item as DataListItem;
        string lateDays = (item.FindControl("lblLateDays") as Label).Text.Trim();
        if (lateDays == "0")
        {
            HtmlTableCell tbl = item.FindControl("tbl") as HtmlTableCell;
            tbl.BgColor = "Red";
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.Add("TDID")
        dt.Columns.Add("TDDesc")
        dt.Columns.Add("taskdate")
        dt.Columns.Add("enddate")
        dt.Columns.Add("TDstatus")
        dt.Columns.Add("latedays")
        dt.Rows.Add(1, "hey", "01/02/2019", "04/05/2019", "Pending", "1")
        dt.Rows.Add(2, "Hi", "05/03/2019", "08/06/2019", "USA", "0")
        Me.dlCustomers.DataSource = dt
        Me.dlCustomers.DataBind()
    End If
End Sub

Protected Sub dlCustomers_OnItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim item As DataListItem = TryCast(e.Item, DataListItem)
        Dim lateDays As String = (TryCast(item.FindControl("lblLateDays"), Label)).Text.Trim()

        If lateDays = "0" Then
            Dim tbl As HtmlTableCell = TryCast(item.FindControl("tbl"), HtmlTableCell)
            tbl.BgColor = "Red"
        End If
    End If
End Sub

Screenshot