Click here to Skip to main content
15,910,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends, my project is based on asp.net c#

am working on GRIDVIEW. in that i have a column name Expirydate

for example :
============
22\08\2012
22\08\2013
15\09\2012
16\06\2012 - This date is expired according to current system date.


So my requirement is, if the ExpiryDate is less than todays date, THE background color of that partciular cell should be changed to red color in GRIDVIEW.


Please help ,

Thanks in ADVANCE.
Posted
Updated 8-Aug-12 19:03pm
v5
Comments
Abdul Quader Mamun 9-Aug-12 0:10am    
Have you tried anything?

Try this
C#
if(DateTime.Compare(Date1,Date2))
{
  GridView1.Rows[index].Cells[index].BackColor = System.Drawing.Color.Red;
}


Regards
Kunjammu
 
Share this answer
 
v2
Comments
Software Engineer 892 8-Aug-12 7:24am    
where should we write this code, I mean in which event
Software Engineer 892 8-Aug-12 7:25am    
If the Date is less than system date, It must change the cackcolor of that particular cell.
Software Engineer 892 8-Aug-12 7:26am    
change the back color of cell, if it is less than system date
Kunjammu 8-Aug-12 7:30am    
u have to write it in RowDataBound event. By using a for loop we can go through all the cells of gridview.so we can easily find the cell. we can compare dates like this also if(DateTime.Today>Date1) { ---- }
Software Engineer 892 8-Aug-12 7:40am    
not working ur code
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            DateTime date = Convert.ToDateTime(GridView1.Rows[i].Cells[0].Text);
            if (date < DateTime.Now)
            {
                GridView1.Rows[i].Cells[0].BackColor = System.Drawing.Color.Red;
            }
            
        }
    }


This is the code. its working for me. change the cell index as u like

Regards
Kunjammu
 
Share this answer
 
Comments
Shwrv 6-May-16 0:16am    
but when gridview row cell become null its given error in the line "DateTime date = Convert.ToDateTime(GridView1.Rows[i].Cells[3].Text);" [String was not recognized as a valid DateTime] @Kunjammu
C#
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

if (e.Row.RowType == DataControlRowType.DataRow)
      {
          
          DateTime Stock =
           Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem,
           "Column_Name"));
          if(Stock > DateTime.Now)
          e.Row.BackColor =System.Drawing.Color.Red;

      }
    }
 
Share this answer
 
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {       
            DateTime expdate = Convert.ToDateTime(e.Rows.Cells[8].Text.Trim());
            if (expdate < DateTime.Now)
            {
                e.Rows.Cells[8].BackColor = System.Drawing.Color.Red;
            }       
    }
 
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