Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello can u please help me on this ..i want to to change the color of the row of gridview if the date is passed ...i am new to programming help me to solve this ...
Posted
Updated 29-Jan-14 19:52pm
v2
Comments
Karthik_Mahalingam 30-Jan-14 1:48am    
column or row ?? make sure
vivek0041 30-Jan-14 1:51am    
sorry my mistake i want to change the color of row not column

Try this..

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {

           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               DateTime date = (DateTime)DataBinder.Eval(e.Row.DataItem, "DateColumnofYourDataSource")   ;
               if (DateTime.Now > date )
                   e.Row.BackColor = System.Drawing.Color.Red;
               else
                   e.Row.BackColor = System.Drawing.Color.Green;
           }
       }
 
Share this answer
 
Comments
vivek0041 30-Jan-14 2:05am    
Specified cast is not valid. this is the error m getting dear
vivek0041 30-Jan-14 2:06am    
DateTime date = (DateTime)DataBinder.Eval(e.Row.DataItem, "duedate"); on this line
Siva Hyderabad 30-Jan-14 2:37am    
5+
Karthik_Mahalingam 30-Jan-14 3:10am    
Thanks SambasivaRao:)
Karthik_Mahalingam 30-Jan-14 3:10am    
post your data source code
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

 Label lbldate= (Label)e.Row.FindControl("id of your label"); //which one contain the date field in Gridview

datetime presentdate;
presentdate=System.Datetime.Now.Tostring();
if (lbldate.text> presentdate)
               {
                  
                   e.Row.BackColor = System.Drawing.Color.FromName("#ffcdea");
               }

else
{
  e.Row.BackColor = System.Drawing.Color.FromName("#FFFFFF");
}

}
}
 
Share this answer
 
v4
Comments
vivek0041 30-Jan-14 1:54am    
i want to do if the date is passes means i have to compare from current date if the date is passed the color should b red or else if the date is greater from current date it should remain as it is
use Gridview1_RowDataBound event

try like this(change in code as per your requirement):-
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int dtrslt = DateTime.Compare(e.Row.DataItem("DATE_COLUMN_NAME"), Date.Now);
        if ( dtrslt < 0)
     e.Row.BackColor = System.Drawing.Color.Red;
                   //Or for hex color
    e.Row.BackColor = ColorTranslator.FromHtml("#D1ECC2");
        else
     e.Row.BackColor = System.Drawing.Color.yellow;
    //Or for hex color
            e.Row.BackColor = ColorTranslator.FromHtml("#E7B3B3")
    }
}
 
Share this answer
 
v2
Comments
vivek0041 30-Jan-14 2:28am    
CS1955: Non-invocable member 'System.Web.UI.WebControls.GridViewRow.DataItem' cannot be used like a method. error
TrushnaK 30-Jan-14 2:45am    
try this:-
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView test = (System.Data.DataRowView)e.Row.DataItem;
DateTime dt = (DateTime) test.Row[4].ToString(); //Add your date column index here
int dtrslt = DateTime.Compare(dt, Date.Now);
if ( dtrslt < 0)
e.Row.BackColor = System.Drawing.Color.Red;
//Or for hex color
e.Row.BackColor = ColorTranslator.FromHtml("#D1ECC2");
else
e.Row.BackColor = System.Drawing.Color.yellow;
//Or for hex color
e.Row.BackColor = ColorTranslator.FromHtml("#E7B3B3")
}
}
vivek0041 30-Jan-14 3:00am    
CS0030: Cannot convert type 'string' to 'System.DateTime'...now this error
vivek0041 30-Jan-14 3:02am    
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView test = (System.Data.DataRowView)e.Row.DataItem;
DateTime dt = (DateTime)test.Row["duedate"].ToString(); //Add your date column index here
int dtrslt = DateTime.Compare(dt, DateTime.Now.ToString());
if ( dtrslt < 0)
e.Row.BackColor = System.Drawing.Color.Red;

else
{
e.Row.BackColor = System.Drawing.Color.Yellow;
}

}
}
this is what i was doing and getting the arror
TrushnaK 30-Jan-14 3:11am    
change this line
DateTime dt = (DateTime) test.Row[4].ToString();
to
DateTime dt = Convert.ToDateTime(test.Row[4].ToString());

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