【问题标题】:Compare date to today on gridview with some empty cells在gridview上用一些空单元格比较日期到今天
【发布时间】:2017-02-28 05:20:31
【问题描述】:

我想更改过期字段的颜色。我想将该列的值与今天的日期进行比较,如果它们已过期,则将它们标记为红色。并非每一行都有一个值。所有这些数据都与网格视图中显示的 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 中绑定了代码,我该如何解决这个问题?

【问题讨论】:

  • 检查e.Row.Cells[10].Text是否为空或为空之前将其转换为日期时间

标签: c# sql asp.net date gridview


【解决方案1】:

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

记住 Cells 从 0 开始索引

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

【讨论】:

  • 那是正确的索引。我有 19 个字段的 Gridview。当 ;nbsp; 时抛出错误。在文本字段中
【解决方案2】:

哇,这是一个简单的修复。我没有检查正确的值。

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;
                }
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多