Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a datagridview and it contains a column call 'Schedule Date'. When I am Running the program the values which are in 'Schedule Date' should be appear like this.
if the value is less than "DateTime.Today" , then that cell should be appear in RED colour and others should appear in GREEN color.
I am confuced. please help me to find a solution for this.
Thanks in Advance.
Posted

XML
<asp:GridView runat="server" ID="gridMain" AutoGenerateColumns="False">
       <Columns>
           <asp:TemplateField>
               <ItemTemplate>
                   <div style='<%# Convert.ToDateTime(DataBinder.Eval(Container.DataItem ,"Schedule_Date")) >= DateTime.Now ? "background-color:green" : "background-color:red" %>'>
                       <%#Eval("Schedule_Date") %>
                   </div>
               </ItemTemplate>
           </asp:TemplateField>
           <asp:BoundField DataField="EmployeeName" HeaderText="Name" />
       </Columns>
   </asp:GridView>
 
Share this answer
 
Comments
thasneemjambi 9-Oct-13 1:42am    
ooops! I am doing this code in C#.Net
CodeBlack 9-Oct-13 1:43am    
mark it as an answer if it helped you.
C#
foreach (DataGridViewRow row in DataGridViewRow1.Rows)
{
                var now = DateTime.Now;
                var cellDate = DateTime.Parse(row.Cells[14].Value.ToString());
                var forTenDays = now.AddDays(+10);

                if (now > cellDate)
                {
                    row.DefaultCellStyle.BackColor = Color.Red;
                }
                else if ((now < cellDate) && (cellDate < forTenDays))
                {
                    row.DefaultCellStyle.BackColor = Color.Yellow;
                }
                else
                {
                    row.DefaultCellStyle.BackColor = Color.LightGreen;
                }
}
 
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