Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8201241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:44:30+00:00 2026-06-07T06:44:30+00:00

I have rows of information stored in a grid view, each containing a date.

  • 0

I have rows of information stored in a grid view, each containing a date. This date is historic and I would like to colour code the date if it is over a year old and also if it is between 10 months and a year old.

Here is the code that I have at the moment:

    foreach(GridViewRow row in gridview.Rows){
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DateTime datepaid = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "date_stored"));
            DateTime date_now = DateTime.Now;
            TextBox1.Text = (date_sub_paid - DateTime.Now).ToString();
            if ((datepaid - DateTime.Now).Days >= -365)
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[4].Font.Bold = true;
            }
            else if ((datepaid - date_now).Days >= -305 && (datepaid - date_now).Days < -365)
            {
                e.Row.ForeColor = System.Drawing.Color.Yellow;
            }
            else
            {
                e.Row.ForeColor = System.Drawing.Color.Black;
            }
        }
    }

There is a flaw in my logic as this the output is not as expected. Would anyone be able to help with this?
Thanks in advance.

EDIT:::

I have edited the code and it now looks like it works correctly:

foreach(GridViewRow row in subtracker_gridview.Rows){
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DateTime datepaid= Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "DATE"));
            DateTime date_now = DateTime.Now;
            TextBox1.Text = (date_now - datepaid).Days.ToString();

            if ((date_now - datepaid).Days >= 365)
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[4].Font.Bold = true;
            }
            else if ((date_now - datepaid).Days >= 335 && (date_now - datepaid).Days < 365)
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Gold;
                e.Row.Cells[4].Font.Bold = true;
            }
            else
            {
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Green;
                e.Row.Cells[4].Font.Bold = true;
            }
        }

However, the first row seems to be ignored and does not get coloured? Any ideas why this is happening? Thanks for your help.

EDIT #2: I removed the foreach loop. This was not needed and caused the first date to be ignored.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:44:32+00:00Added an answer on June 7, 2026 at 6:44 am

    Second condition is never true.
    There is no number that is greater than -305 and less than -365
    Also if the first half of the second condition is true it won’t be reached because the first condition would be true too.
    Save yourself the confusion and subtract now from the parsed date and deal with positive numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have rows like this... | NAME | RED | BLUE | GREEN |
I quite often have rows of code like the following: UPDATE my_table SET name
I have a grid view with a nested text box in it. I would
I have a stored proc that returns 4 rows in a table. Each row
I have rows in a MySQL database, and each has a timestamp value. (ie
I have a grid with several thousand rows that can be filtered and sorted.
I have a stored procedure that inserts batches of millions of rows, emerging from
I have a JSON file that I would like to create an multi-dimensional array
I am using the following code to store a person's information from a grid
I have table with millions of records (rows). And in this table there is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.