Reputation: 267
i want to show if renewal date column date greater than todays date then it should highlight with background color = red . here im not getting the value in cell 7. the value is in renewal date col - 15-02-2014
<asp:TemplateField HeaderText="Renewal Date" >
<ItemTemplate>
<a href='ChangeRenewaldate.aspx?Linkid=<%#Eval ("LinkId")%>'>
<asp:Label ID="lblRenewal" runat="server" Text='<%# Eval("RenewalDate","{0:dd-MM-yyyy}") %>'></asp:Label> </ItemTemplate>
</asp:TemplateField>
protected void GrdV_Projects_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!string.IsNullOrEmpty(e.Row.Cells[7].Text))
{
if (e.Row.Cells[7].Text > DateTime.Now.ToString())
{
e.Row.Cells[7].BackColor = System.Drawing.Color.Red;
}
else
{
e.Row.Cells[7].BackColor = System.Drawing.Color.Green;
}
}
}
}
Upvotes: 0
Views: 1134
Reputation: 460138
If you use TemplateFields
you have to use FindControl
to get a reference to a control, if you use BoundFields
you have to use e.Row.Cell[index].Text
. So you could use following:
Label lblRenewal = (Label) e.Row.FindControl("lblRenewal");
to find the Label
and then parse it's Text
.
But in this case you should prefer using the DataSource
of the GridViewRow
to get the real DateTime
instead of parsing strings:
protected void GrdV_Projects_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
DateTime renewalDate = row.Field<DateTime>("RenewalDate");
if (renewalDate.Date > DateTime.Today)
e.Row.Cells[7].BackColor = System.Drawing.Color.Red;
else
e.Row.Cells[7].BackColor = System.Drawing.Color.Green;
}
}
Upvotes: 2