ali raza

C#, .Net , and my thoughts

how to change the color of datagridviews row at difference in two dates

use the following Code in application

//This method returns the no of days between today and date comes from database
private int GetDateDifference(DateTime _StartDateTime )
DateTime startDate = _StartDateTime;
// End date
DateTime endDate = DateTime.Now;
// Time span
TimeSpan diffDate = endDate.Subtract ( startDate );

return diffDate.Days;
//Gridview Row Data Bound Event
protected void grdDetail_RowDataBound(object sender, GridViewRowEventArgs e)

if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer )

Label _lbl = (Label)e.Row.FindControl("lblDate");

if (_lbl != null)
_lbl.Text = DataBinder.Eval(e.Row.DataItem, "TRANSACTION DATE").ToString();
int intDays = GetDateDifference(Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "TRANSACTION DATE")));
if (intDays > 2)
// Setting the color of row
e.Row.BackColor = System.Drawing.Color.Red;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: