title: Colouring an item based on date in grid view - c# asp.net webforms
body: <p>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.</p>
  
  <p>Here is the code that I have at the moment:</p>
  
  <pre><code>    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 &gt;= -365)
              {
                  e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                  e.Row.Cells[4].Font.Bold = true;
              }
              else if ((datepaid - date_now).Days &gt;= -305 &amp;&amp; (datepaid - date_now).Days &lt; -365)
              {
                  e.Row.ForeColor = System.Drawing.Color.Yellow;
              }
              else
              {
                  e.Row.ForeColor = System.Drawing.Color.Black;
              }
          }
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>EDIT:::</p>
  
  <p>I have edited the code and it now looks like it works correctly:</p>
  
  <pre><code>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 &gt;= 365)
              {
                  e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                  e.Row.Cells[4].Font.Bold = true;
              }
              else if ((date_now - datepaid).Days &gt;= 335 &amp;&amp; (date_now - datepaid).Days &lt; 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;
              }
          }
  </code></pre>
  
  <p>However, the first row seems to be ignored and does not get coloured? Any ideas why this is happening? Thanks for your help.</p>
  
  <p>EDIT #2: I removed the foreach loop. This was not needed and caused the first date to be ignored.</p>