Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Friends, Am working on Asp.net c#, SqlServer 2005.

In my SqlServer 2005 Database I have a table with two dates DateFrom and DateTo, for this both datates is used datatypes as nvarchar(10)....Because i need to display as 2013/01/16. its fine now

I need a Condition on this dates.....please help me.

I have Two Dates...., as DateFrom and DateTo

This data is displaying in Asp.net Gridview...In Gridview the DateFrom is Cell[4] and DateTo is Cell[5]....and now just i want to change the colors of cells based on date conditions.


DateFrom Date To
----------- ------------
2012/10/28 2013/03/30
2012/11/19 2013/03/21
2012/12/20 2013/03/26
2013/01/11 2013/01/12


C#
If DateFrom <= todays date and datefrom >= todays date
{
item.cells[4].BackColor = color.FromName ("#77FF77");
item.cells[5].BackColor = color.FromName ("#77FF77");
}

else if DateFrom  < Todays date and dateto < todaysdate
{
item.cells[4].BackColor = color.FromName ("#FF6A6A");
item.cells[5].BackColor = color.FromName ("#FF6A6A");
}
else if datefrom >= todays date and dateto >= todaysdate
{
item.cells[4].BackColor = color.FromName ("#77FF77");
item.cells[5].BackColor = color.FromName ("#77FF77");
}

here #77FF77 indicates Green color
and #FF6A6A indicates red color.

Please help me, i need good else condition.

Thanks.
Posted
Updated 15-Jan-13 20:39pm
v2

C#
protected void grdA_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Decimal Val= GetCellValue(5, e);
            if (Val!= null)
            {
               if (Val< 0)
               {
                 ColorThisCellBackColor(5, e, Color.FromArgb(255, 235, 214));         }
            } 
        }
    }


C#
private void ColorThisCellBackColor(int ColumnIndex, GridViewRowEventArgs e, Color ColorName)
   {
       e.Row.Cells[ColumnIndex].BackColor = ColorName;
   }


VB
private Decimal GetCellValue(int ColumnIndex, GridViewRowEventArgs e)
 {
     try
     {
         Decimal cellval;
         if (HttpUtility.HtmlDecode(e.Row.Cells[ColumnIndex].Text).Trim() == string.Empty)
         {
             cellval = 0;
         }
         else
         {
             cellval = Convert.ToDecimal(HttpUtility.HtmlDecode(e.Row.Cells[ColumnIndex].Text).Trim());
         }

         return cellval;

     }
     catch (Exception)
     {

         throw;
     }

 }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900