2016-10-18 47 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

相关问题