c#
  • asp.net
  • gridview
  • 2015-09-28 24 views 0 likes 
    0

    我想顯示續訂日期列日期是否大於今天的日期,那麼它應該用背景顏色=紅色突出顯示。在這裏我不是在小區7所獲得的價值值在續約日期山坳 - 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; 
           } 
          } 
         } 
        } 
    

    回答

    2

    如果使用TemplateFields你必須使用FindControl去控制一個參考,如果您使用BoundFields你必須使用e.Row.Cell[index].Text。所以,你可以使用下列內容:

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

    找到Label,然後分析它的Text

    但在這種情況下,你應該更喜歡使用DataSourceGridViewRow的才能獲得真正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; 
        } 
    } 
    
    相關問題