在RowDataBound事件中进行操作
protected void GVContentList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
{
System.IFormatProvider format = new System.Globalization.CultureInfo("zh-CN", true);
Label lblSex = e.Row.FindControl("lblSex") as Label;
Label lblbirth = e.Row.FindControl("lblbirth") as Label;
Label lblPatientName = e.Row.FindControl("lblPatientName") as Label;
Label lblPatient = e.Row.FindControl("lblPatient") as Label;
lblPatient.Text = lblPatientName.Text;
if (lblSex.Text != "")
{
if (Convert.ToInt32(lblSex.Text.ToString()) == 1)
{
lblPatient.Text += ",男";
}
else
{
lblPatient.Text += ",女";
}
}
if(lblbirth.Text!="")
{
int birthYear = 0;
birthYear = Convert.ToInt32(lblbirth.Text.Substring(0, 4));
int age = System.DateTime.Now.Year - birthYear;
lblPatient.Text += ","+age+"岁";
}
Label lblScTime = e.Row.FindControl("lblScTime") as Label;
DateTime st = DateTime.ParseExact(lblScTime.Text, "yyyyMMdd", format);
string sctime = st.Year + "-" + st.Month + "-" + st.Day;
lblScTime.Text = sctime;
Label lblExamstate = e.Row.FindControl("lblExamstate") as Label;
Label lblScheduleDate = e.Row.FindControl("lblScheduleDate") as Label;
Label lblScheduleTime = e.Row.FindControl("lblScheduleTime") as Label;
Label lbloperateDate = e.Row.FindControl("lbloperateDate") as Label;
Label lbloperateTime = e.Row.FindControl("lbloperateTime") as Label;
Label lblState = e.Row.FindControl("lblState") as Label;
if (lblExamstate.Text == "0" || lblExamstate.Text == "10")
{
lblState.Text = "<a class='Cred'>未到</a>";
}
else
{
//比较时间段 预约日期和登记日期相同
string[] scTime = lblScheduleTime.Text.ToString().Split('-');
string rgtime = lbloperateTime.Text.ToString().Substring(0, 4);
rgtime = rgtime.Insert(2, ":");
DateTime t1 = DateTime.ParseExact(lblScheduleDate.Text, "yyyyMMdd", format);
DateTime t2 = DateTime.ParseExact(lbloperateDate.Text, "yyyyMMdd", format);
string schetime = t1.Year + "-" + t1.Month + "-" + t1.Day + " " + scTime[1];
string schetime1 = t1.Year + "-" + t1.Month + "-" + t1.Day + " " + scTime[0];
string opertime = t2.Year + "-" + t2.Month + "-" + t2.Day + " " + rgtime;
DateTime dt1 = Convert.ToDateTime(schetime);
DateTime dt2 = Convert.ToDateTime(opertime);
DateTime dt3 = Convert.ToDateTime(schetime1);
TimeSpan ts = dt2-dt1;
string tsstring = "";
string showtime = "";
if (ts.Days > 0 || ts.Hours > 0 || ts.Minutes > 0)
{
if (ts.Days != 0)
{
tsstring += ts.Days + "d";
showtime += ts.Days + "天";
}
if (ts.Hours != 0)
{
tsstring += ts.Hours + "h";
showtime += ts.Hours + "小时";
}
if (ts.Minutes != 0)
{
tsstring += ts.Minutes + "m";
showtime += ts.Minutes + "分钟";
}
lblState.Text = "<a class='user_manage_a2'>迟到 " + showtime + "</a>";
}
else
{
ts = dt3 - dt2;
if (ts.Days != 0)
{
tsstring += ts.Days + "d";
showtime += ts.Days + "天";
}
if (ts.Hours != 0)
{
tsstring += ts.Hours + "h";
showtime += ts.Hours + "小时";
}
if (ts.Minutes != 0)
{
tsstring += ts.Minutes + "m";
showtime += ts.Minutes + "分钟";
}
lblState.Text = "<a class='user_manage_a2'>早到 " + showtime + "</a>";
}
}
//改变背景色
if (e.Row.RowIndex % 2 != 0)
{
//设置偶数行背景色
e.Row.CssClass = "user_manage_tableTr";
}
}
}