In .aspx.cs file Write the following code:
private static int count = 0;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
SqlConnection cn = new SqlConnection("data source=your sql server name;initial catalog=your db name;integrated security=sspi");
string qry = "select * from employees";
SqlCommand cmd = new SqlCommand(qry, cn);
cn.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
cn.Close();
Session["ds"] = ds;
gvEmps.DataSource = ds;
DataTable dt = ds.Tables[0];
DataColumn dc = new DataColumn("Eligibility",typeof(string));
dt.Columns.Add(dc);
gvEmps.DataBind();
}
}
protected void gvEmps_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (count > 0)
{
GridViewRow gvr = e.Row;
TableCell tc = gvr.Cells[3];
string data = tc.Text;
if (data != " "&&data!="doj")
{
DateTime dt = Convert.ToDateTime(data);
int yrsDiff = DateTime.Now.Year - dt.Year;
if (yrsDiff >= 5)
{
TableCell tc1 = gvr.Cells[4];
tc1.Text = "Eligible";
tc1.Style.Add("color", "red");
}
}
}
if (count == 0)
{
count = 1;
}
}
Table Structure:
private static int count = 0;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
SqlConnection cn = new SqlConnection("data source=your sql server name;initial catalog=your db name;integrated security=sspi");
string qry = "select * from employees";
SqlCommand cmd = new SqlCommand(qry, cn);
cn.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
cn.Close();
Session["ds"] = ds;
gvEmps.DataSource = ds;
DataTable dt = ds.Tables[0];
DataColumn dc = new DataColumn("Eligibility",typeof(string));
dt.Columns.Add(dc);
gvEmps.DataBind();
}
}
protected void gvEmps_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (count > 0)
{
GridViewRow gvr = e.Row;
TableCell tc = gvr.Cells[3];
string data = tc.Text;
if (data != " "&&data!="doj")
{
DateTime dt = Convert.ToDateTime(data);
int yrsDiff = DateTime.Now.Year - dt.Year;
if (yrsDiff >= 5)
{
TableCell tc1 = gvr.Cells[4];
tc1.Text = "Eligible";
tc1.Style.Add("color", "red");
}
}
}
if (count == 0)
{
count = 1;
}
}
Table Structure:
eid | fname | age | doj | Eligibility |
---|---|---|---|---|
1 | rijin | 23 | 04-Oct-09 12:00:00 AM | Eligible |
2 | kishore | 23 | 04-Oct-14 12:00:00 AM |