使用一些空单元格在gridview上比较日期到今天

时间:2016-10-18 19:35:33

标签: c# sql asp.net date gridview

我想更改到期字段的颜色。我想将该列的值与今天的日期进行比较,如果它们已过期则将其标记为红色。并非每一行都有值。所有这些数据都与gridview中显示的SQL数据源相关联。当我尝试通过on DataRowBound执行代码时,它告诉我“字符串未被识别为有效日期时间”这里有任何帮助吗?我希望它影响Expiration字段和Action

谢谢!

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中绑定了代码,那么我该怎么做才能解决这个问题呢?

2 个答案:

答案 0 :(得分:0)

你确定Cells [10]包含日期时间吗?

请记住,Cells在0开始索引

运行debug并获取Cells [10] .Text的值并检查是否正确

答案 1 :(得分: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;
                }
            }
        }
    }
相关问题