Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi frndss !!

on my webpage i have Gridview, am suing asp.net c#.

now i want to change the back color of my cell, if the date is less than todays date.

I have column name as ExpiryDate...Suppose if in gridview cell, the date is less than todays date. then the cell back color must change to RED.

This cell is cell[6] in gridview.

In Database table i took ExpiryDate column Datatype as Datetime

Please help me, how to do this.

Thanks.
Posted
Comments
David_Wimbley 27-May-13 1:57am    
Can you post some code. With this being a website my initial thought is to just add a css class so that way you style it differently.

But obviously without seeing what you've done/how you've implemented what you are talking about i have no clue where to start.

Off the top of my head there are 5 ways i can think of doing what you are asking.

1) Css class
2) Add style attribute at run time with background color based on requirement

so on and so forth

Dear Friend,

Always search google first before posting your question here.

About 126,000 on how to change cell background color in gridview

In the below link, Solution 2 is the answer to your question:-

change cell color of GRIDVIEW based on system date (Solution 2)

Please don't forget to mark this as your answer if it helps you out.

Regards

Varun Sareen
 
Share this answer
 
Dear Friend,

Always search google first before posting your question here.

About 126,000 on how to change cell background color in gridview

In the below link, Solution 2 is the answer to your question:-

change cell color of GRIDVIEW based on system date (Solution 2)

Please don't forget to mark this as your answer if it helps you out.

Regards

Varun Sareen
 
Share this answer
 
C#
DateTime _dt1 = Convert.ToDateTime(GridView1.Rows[0][6]);
DateTime _dt2 = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy"));
if (_dt1 < _dt2)
{
    GridView1.Rows[0][6].BackColor = System.Drawing.Color.Red;
}
 
Share this answer
 
v2
Comments
lukeer 27-May-13 2:18am    
There's no good in getting the current DateTime (Now), converting it to a string and parsing it back to a DateTime. Just compare to DateTime.Now instead.

And use a code block (FTFY).

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