≡ Menu

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;
}
}
}

Enjoy

{ 0 comments… add one }

Leave a Comment