Kadaw Tomtom
Kadaw Tomtom

Reputation: 73

How to highlight gridview column where date older than

I have a gridview that is linked to a datasource. I have a column called patched_date.

How do I highlight that column where the date is older than 30 days, 60 days, etc.

I'm not a coder. I did find somethings similar, but I dont understand it. Is there a different code that I can use?

Ok, I can't figure this out. Why am i getting "String was not recognized as a valid DateTime." when editing the date column? I'm guessing it has something to do with the way it interprets the data?

How can I fix this?

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

    }

Upvotes: 0

Views: 893

Answers (1)

Marco Bong
Marco Bong

Reputation: 690

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

Upvotes: 1

Related Questions