C# 如果列日期大于当前日期,如何在栅格视图中更改背景颜色
我想显示如果续订日期列date大于todays date,那么它应该以背景色=红色突出显示。这里我没有得到单元格7中的值。该值为更新日期col-15-02-2014C# 如果列日期大于当前日期,如何在栅格视图中更改背景颜色,c#,asp.net,gridview,C#,Asp.net,Gridview,我想显示如果续订日期列date大于todays date,那么它应该以背景色=红色突出显示。这里我没有得到单元格7中的值。该值为更新日期col-15-02-2014 <asp:TemplateField HeaderText="Renewal Date" > <ItemTemplate> <a href='ChangeRenewaldate.aspx?Linkid=<%#Eval ("LinkId")%>'
<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;
}
}
}
}
受保护的void GrdV_Projects_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
如果(!string.IsNullOrEmpty(e.Row.Cells[7].Text])
{
if(例如,Row.Cells[7].Text>DateTime.Now.ToString())
{
e、 Row.Cells[7]。BackColor=System.Drawing.Color.Red;
}
其他的
{
e、 Row.Cells[7]。BackColor=System.Drawing.Color.Green;
}
}
}
}
如果使用模板字段
则必须使用FindControl
获取对控件的引用,如果使用边界字段
则必须使用e.Row.Cell[index].Text
。因此,您可以使用以下内容:
Label lblRenewal = (Label) e.Row.FindControl("lblRenewal");
找到标签
,然后解析它的文本
但是在这种情况下,您应该更喜欢使用GridViewRow
的DataSource
来获取真正的DateTime
,而不是解析字符串:
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;
}
}
受保护的void GrdV\u Projects\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
DataRow row=((DataRowView)e.row.DataItem).row;
DateTime renewalDate=行字段(“renewalDate”);
如果(renewalDate.Date>DateTime.Today)
e、 Row.Cells[7]。BackColor=System.Drawing.Color.Red;
其他的
e、 Row.Cells[7]。BackColor=System.Drawing.Color.Green;
}
}