2016-10-18 98 views
0

我想改變到期字段的顏色。我想將該列的值與今天的日期進行比較,如果過期,則將其標記爲紅色。不是每一行都有一個值。所有這些數據都與GridView中顯示的SQL數據源相關聯。當我嘗試通過DataRowBound執行代碼時,它告訴我「字符串未被識別爲有效日期時間」在這裏有任何幫助?我希望它影響過期字段和行動比較日期到今天在一些空單元格的gridview

謝謝!

protected void GridView1_DataBound(object sender, EventArgs e) 
    { 
     if (GridView1.Rows[0].Cells[0].Text.Equals("Vendor")) 
      GridView1.Rows[0].Visible = false; 

     colorRed(); 
    } 
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      DateTime myDate = Convert.ToDateTime(e.Row.Cells[10].Text); 
      if (DateTime.Now < myDate)//10 is for the expired field 
      { 
       e.Row.ForeColor = System.Drawing.Color.Red; 
      } 
     } 
    } 

我有代碼綁定在html中,所以我能做些什麼來解決這個問題?

+0

檢查'e.Row.Cells [10] .Text'爲空白或空** **之前將其轉換爲DateTime –

回答

0

哇,這是一個簡單的解決。我沒有檢查正確的值。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      if (!(e.Row.Cells[10].Text.Equals("&nbsp;")))//Needed to check for this 
      { 
       DateTime myDate = Convert.ToDateTime(e.Row.Cells[10].Text); 
       if (DateTime.Now < myDate) 
       { 
        e.Row.ForeColor = System.Drawing.Color.Red; 
       } 
      } 
     } 
    } 
0

您確定Cells [10]包含日期時間嗎?

請記住,細胞在0

運行調試開始索引,並獲得細胞[10]。文本的價值,並檢查是否是正確的

+0

即正確的索引。我有19個字段的Gridview。錯誤被拋出時; nbsp;在文本字段中 – HelixTitan

相關問題