如何突出显示日期早于的gridview列

时间:2016-03-03 00:55:25

标签: c# asp.net gridview

我有一个链接到数据源的gridview。我有一个名为patched_date的列。

如何突出显示日期超过30天,60天等的列

我不是编码员。我确实找到了类似的东西,但我不明白。我可以使用不同的代码吗?

好的,我无法弄清楚这一点。为什么我得到“字符串未被识别为有效的DateTime”。在编辑日期栏时?我猜它与解释数据的方式有关?

我该如何解决这个问题?

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
        DateTime date_patched = Convert.ToDateTime(GridView2.Rows[i].Cells[3].Text);
        if (date_patched < DateTime.Now)
        {
            GridView2.Rows[i].Cells[3].BackColor = System.Drawing.Color.Red;
        }

    }

1 个答案:

答案 0 :(得分:1)

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Loop throught your number of rows from gridview data
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            //Datetime from your gridview column no.4 (0,1,2,3)
            DateTime date_patched = Convert.ToDateTime(GridView2.Rows[i].Cells[3].Text);

            //Here you need to modify
            //if date_patched older than 30 days and later than 60 days
             if(date_patched < DateTime.Now.AddDays(-30) && date_patched > DateTime.Now.AddDays(-60)) 
             { 

                 //do something
                 GridView2.Rows[i].Cells[3].BackColor = System.Drawing.Color.Red;
             }
             //if older than 60days 
             else if (date_patched < DateTime.Now.AddDays(-60))     
             {
                 //do something
             }
        }