Click here to Skip to main content
15,916,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi frnds,

am working on Asp.net Gridview using C# and Sqlserver 2005.

my requirement is :

on my webpage i have Gridview, in this i have i column expiry date, if expiry date is less than todays date. the cell must become red.
This is cell[4] in my gridview.

Please can you help me how to do this,

Thanks.
Posted
Comments
Manfred Rudolf Bihy 22-May-13 19:12pm    
"frnds", "Software Engineer", hmmmm.
Software Engineer 892 22-May-13 19:30pm    
This is my code, the complete row is becoming red,,, i need only 4th cell.

protected void GvManageUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "ExpiryDate")) < DateTime.Now)
{
e.Row.BackColor = System.Drawing.Color.Red;
e.Row.ForeColor = System.Drawing.Color.White;
}
else
{
e.Row.BackColor = System.Drawing.Color.White;
e.Row.ForeColor = System.Drawing.Color.Black;

}

}


Please help thanks...

C#
protected void gvtest_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        DateTime tempDate = new DateTime();
        try { tempDate = Convert.ToDateTime(e.Row.Cells[4].Text.Trim()); }
        catch(Exception ex){ }
        
        if(tempDate < DateTime.Now)
           e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
        
    }
}


In the above code, line:
e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
will make the cell background color red. Change No. 4 to whatever column number or name you want.

--Nayan
 
Share this answer
 
Hi,

You can use OnRowDataBound event of gridview.
Example:

In the HTML part;
<asp:gridview runat="server" id="gvtest" onrowdatabound="gvtest_OnRowDataBound" xmlns:asp="#unknown"></asp:gridview>


In the code behind;
C#
protected void gvtest_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        DateTime tempDate = new DateTime();
        try { tempDate = Convert.ToDateTime(e.Row.Cells[4].Text.Trim()); }
        catch(Exception ex){ }
        
        if(tempDate < DateTime.Now)
           e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
        
    }
}


Hope this helps.
Happy coding..!!

-Nayan
[Please do not forget to mark the answer as correct if it resolved your issue]
 
Share this answer
 
Comments
Software Engineer 892 22-May-13 19:42pm    
Sorry, this becomes all the column red.....i need if the date is less than todays date.
My column name is ExpiryDate.

Please help thanks........
Nayan Ambaliya 22-May-13 19:54pm    
Mate,

Please closely have a look at the solution.
It clearly says that e.Row.Cells[4].BackColor. This will only change the cells color.

Please paste your code here if you are still having issues...
Nayan Ambaliya 22-May-13 19:55pm    
Apologies for asking your code..
In your code, you have e.Row.BackColor..
This will change the color of the whole row.

Please see the solution code above.
It has e.Row.Cells[4].BackColor.
This will change the color of the cell only.
Software Engineer 892 22-May-13 19:56pm    
Yes boss, if the ExpiryDate columns date is less than todays date, only that particular cell must become red.

Note : I want to make the cell back color red, if the cell is less than todays date.

Please help.
Thanks7872 23-May-13 0:29am    
Dont repost the answers,Use Improve question link below your answer.
Why dont you make use of DateTime.Compare Method? Refer to below link

DateTime.Compare Method[^]

That article shows code snippet something like below:

DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;

if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";


Do you get idea how to compare dates from the above code block? Try to implement it in your GvManageUsers_RowDataBound Event.

Happy coding.! :laugh:
 
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