【问题标题】:Null Row Cell in GridView with 'Date' typeGridView 中具有“日期”类型的空行单元格
【发布时间】:2016-08-30 20:53:55
【问题描述】:

我已在 gridview 中使用 rowdatabind 代码通过彩色行显示那些日期已经过期的过期日期警报。我表的列 ExpDate 类型是“日期”。它运作良好,但现在的问题是 - 当任何行单元格变为空时,它的给定错误。我的 gridviewrowDatabound 代码是-

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        DateTime date = Convert.ToDateTime(GridView1.Rows[i].Cells[8].Text);
        if (date < DateTime.Now)
        {
            GridView1.Rows[i].BackColor = System.Drawing.Color.Red;
            GridView1.Rows[i].Visible = true;
        }
        else
        {
            GridView1.Rows[i].Visible = false;
        }
    }


}

显示错误:“字符串未被识别为有效的 DateTime”,对于代码行-“DateTime date = Convert.ToDateTime(GridView1.Rows[i].Cells[8].Text);” 现在我能做什么。任何人都可以给我一个想法,拜托!!

【问题讨论】:

  • 您遇到的错误是什么?
  • 放置一个调试器并验证发生异常时您为e.Row.Cells[3].Text 获得的值
  • 当我点击这个编辑链接时,考虑到这种类型的错误。调试此行后显示错误此行[previous commnent] @irvgk
  • 也分享编辑命令代码
  • OnRowEditing 事件处理程序中,您必须设置EditIndex,然后将数据重新绑定到GridView。请在以下帖子中查看我的回答:stackoverflow.com/questions/36827111/….

标签: c# asp.net


【解决方案1】:

你为什么不在客户端做“绘画”工作?

我使用这个代码(把它放在 aspx 文件中):

 <script language="javascript" type="text/javascript">
    function pageLoad() {
        $('#MainContent_gridView td').each(function () {

            //YOUR VALIDATION
            if ($(this).html() == "STATUS 1") {
                $(this).attr("class", "text-info");
            }

            if ($(this).html() == "STATUS 2") {
                $(this).attr("class", "text-error");
            }

            if ($(this).html() == "STATUS 3") {
                $(this).attr("class", "text-success");
            }
        });
    }
</script>

小心使用“pageLoad”方法,如果您没有在母版页中使用“pageLoad”,请不要担心。 我更喜欢在客户端做“绘画”工作,因为我不喜欢去服务器只是为了我可以在客户端做的事情

【讨论】:

  • 我无法理解这段 js 代码。我的问题是“日期”类型的“ExpDate”列单元格中的空值。
  • 贴出GridView的设计代码(
猜你喜欢
  • 1970-01-01
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 2017-02-28
  • 2021-12-14
相关资源
最近更新 更多