【问题标题】:how to change back ground colour in grid view if column date is more then todays date如果列日期大于今天的日期,如何在网格视图中更改背景颜色
【发布时间】:2015-09-28 19:15:54
【问题描述】:

我想显示如果续订日期列日期大于今天的日期,那么它应该以背景颜色 = 红色突出显示。这里我没有得到单元格 7 中的值。该值在更新日期 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;
                }
            }
        }
    }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    如果使用TemplateFields,则必须使用FindControl 来获取对控件的引用,如果使用BoundFields,则必须使用e.Row.Cell[index].Text。所以你可以使用以下内容:

    Label lblRenewal = (Label) e.Row.FindControl("lblRenewal");
    

    找到Label,然后解析为Text

    但在这种情况下,您应该更喜欢使用GridViewRowDataSource 来获取真正的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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 2015-03-11
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多