GridView根据数据库返回的值,在显示到界面前对相关数据进行处理后显示

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值