Gridview合并单元格

Code

实例:实现效果如下:

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using XNCJWC.BLL;
using XNCJWC.Model;
using XNCJWC.Utility;
namespace XNCJWC.Web
{
    public partial class GradeManage_GradeQueryPersonal : System.Web.UI.Page
    {
        private XNCJWC.BLL.Common commonBll = new XNCJWC.BLL.Common();//公共业务逻辑
        private xscjBLL xscjBll = new xscjBLL();//学生成绩业务逻辑
        string strWhere = "";
        double sum1 = 0;
        double sum2 = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                //绑定学年下拉框
                commonBll.BindYearDownList(ddlstXn);
                //绑定学期下拉框
                commonBll.BindTermDownList(ddlstXq);
                //初始化学年
                this.ddlstXn.SelectedValue = Utility.Common.GetCurrentYear();
                //初始化学期
                this.ddlstXq.SelectedValue = Utility.Common.GetCurrentTerm();

            }
        }
        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            strWhere = GetQueryWhere();
            //绑定jv
            strWhere += " order by xn,xq";
            Query(strWhere);
        }
        /// <summary>
        /// 查询
        /// </summary>
        private void Query(string strWhere)
        {
          
            DataTable dt = xscjBll.GetList(strWhere).Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.Panel1.Visible = true;
                this.lblTitle.Text = "学生个人成绩单";
                this.lblInfo.Text = "学号: " + dt.Rows[0]["xh"].ToString() + "   姓名: " + dt.Rows[0]["xm"].ToString();
                this.gvGradeQuery.DataSource = dt;
                this.gvGradeQuery.DataBind();
                GroupRows(gvGradeQuery, 1);
                GroupRows(gvGradeQuery, 0);
               
            }
            else
            {

                ScriptManager.RegisterStartupScript(this.gvGradeQuery, gvGradeQuery.GetType(), "NoRecord", "alert('没有该学生该学年学期的课程成绩!');", true);
            }
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private string GetQueryWhere()
        {
            string strWhere = "";
            if (this.ddlstXn.SelectedValue != null && !this.ddlstXn.SelectedValue.Equals(""))
                strWhere = " xn ='" + this.ddlstXn.SelectedValue + "' and ";
            if (this.ddlstXq.SelectedValue != null && !this.ddlstXq.SelectedValue.Equals(""))
                strWhere += " xq ='" + this.ddlstXq.SelectedValue + "' and ";
            if (txtStuXh.Text.Trim().Length > 0 && txtStuXh.Text != null)
                strWhere += "  xh='" + this.txtStuXh.Text.Trim() + "' and";
            if (strWhere.Trim().Length > 0)
                strWhere = strWhere.Substring(0, strWhere.LastIndexOf("and"));
            return strWhere;

        }
        /// <summary>
        /// 设置学期显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
     
        protected void gvGradeQuery_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                // e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#C0FAFF'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                if (!e.Row.Cells[7].Text.Equals("")&&Convert.ToDecimal(e.Row.Cells[7].Text) < 60  )
                    e.Row.Cells[7].ForeColor = System.Drawing.Color.Red;
              
                switch (e.Row.Cells[1].Text.Trim())
                {
                    case "1":                
                        
                         e.Row.Cells[1].Text ="春季学期";
                         e.Row.Cells[0].Text = (Convert.ToInt32(e.Row.Cells[0].Text) - 1).ToString() + "-" + e.Row.Cells[0].Text;
                         break;                    
                    case "2":
                        e.Row.Cells[0].Text = (Convert.ToInt32(e.Row.Cells[0].Text) - 1).ToString() + "-" + e.Row.Cells[0].Text;
                        e.Row.Cells[1].Text = "夏季学期";
                     
                        break;
                    case "3":
                      
                        e.Row.Cells[1].Text = "秋季学期";
                        e.Row.Cells[0].Text = e.Row.Cells[0].Text + "-" + (Convert.ToInt32(e.Row.Cells[0].Text) + 1).ToString();
                        break;
                    case "4":
                        e.Row.Cells[0].Text = e.Row.Cells[0].Text + "-" + (Convert.ToInt32(e.Row.Cells[0].Text) + 1).ToString();
                        e.Row.Cells[1].Text = "冬季学期";
                        break;
                }
               
              


                if (e.Row.Cells[5].Text == "0")
                    e.Row.Cells[5].Text = "";
                if (e.Row.Cells[6].Text == "0")
                    e.Row.Cells[6].Text = "";
                if (!e.Row.Cells[3].Text.Equals(""))
                {
                    sum1 += Convert.ToDouble(e.Row.Cells[3].Text);
                }
                if (!e.Row.Cells[8].Text.Equals(""))
                {

                    sum2 += Convert.ToDouble(e.Row.Cells[8].Text);
                }
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "总计:";
                e.Row.Cells[3].Text = sum1.ToString();
                e.Row.Cells[8].Text = sum2.ToString();

            }

           
        }


        /// <summary>
        /// 合并GridView中某列相同信息的行(单元格)
        /// </summary>
        /// <param name="GridView1">GridView</param>
        /// <param name="cellNum">第几列</param>
        public static void GroupRows(GridView GridView1, int cellNum)
        {
          
                int i = 0, rowSpanNum = 1;
                while (i < GridView1.Rows.Count - 1)
                {
                    GridViewRow gvr = GridView1.Rows[i];

                    for (++i; i < GridView1.Rows.Count; i++)
                    {
                        GridViewRow gvrNext = GridView1.Rows[i];
                        if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
                        {
                            gvrNext.Cells[cellNum].Visible = false;
                            rowSpanNum++;
                        }
                        else
                        {
                            gvr.Cells[cellNum].RowSpan = rowSpanNum;
                          
                            rowSpanNum = 1;
                            break;
                        }

                        if (i == GridView1.Rows.Count - 1)
                        {
                            gvr.Cells[cellNum].RowSpan = rowSpanNum;
                          
                        }
                    }
                }
           
        }


        /// <summary>
        /// 查询学生所有成绩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAllGrade_Click(object sender, EventArgs e)
        {
            if (txtStuXh.Text.Trim().Length > 0 && txtStuXh.Text != null)
            {
                strWhere = "  xh='" + this.txtStuXh.Text.Trim() + "'";
                strWhere += " order by xn,xq";
                Query(strWhere);
            }

        }
}
}

posted @ 2009-05-10 01:43  Blue Sky ......  阅读(1177)  评论(0编辑  收藏  举报