asp:GridView 或是 asp:DataGrid的表头合并--备忘!!!

经常需要合并表头,但GridView 和DataGrid有点小区别,不过都是很小的问题:

GridView:

     在页面上 是 OnRowCreated="gvStudent_RowCreated"  属性;

     protected void gvStudent_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {

                //第一种方法,用<Table>来写的
                //LiteralControl lc1 = new LiteralControl(GetGradeClassCellHead("序号"));
                //e.Row.Cells[0].Controls.Add(lc1);

                //LiteralControl lc2 = new LiteralControl(GetGradeClassCellHead("科室"));
                //e.Row.Cells[1].Controls.Add(lc2);

                //LiteralControl lc3 = new LiteralControl(GetGradeClassCellHead("教师姓名"));
                //e.Row.Cells[2].Controls.Add(lc3);


                //e.Row.Cells[3].ColumnSpan = 7;
                //LiteralControl lc = new LiteralControl(GetMegCell());
                //e.Row.Cells[3].Controls.Add(lc);
                //e.Row.Cells[4].Visible = false;
                //e.Row.Cells[5].Visible = false;
                //e.Row.Cells[6].Visible = false;
                //e.Row.Cells[7].Visible = false;
                //e.Row.Cells[8].Visible = false;
                //e.Row.Cells[9].Visible = false;
                //LiteralControl lcCheck = new LiteralControl(GetGradeClassCellHead("总计"));
                //e.Row.Cells[10].Controls.Add(lcCheck);

 

 

                TableCellCollection tcHeader = e.Row.Cells;
                tcHeader.Clear();

                tcHeader.Add(new TableHeaderCell());
                tcHeader[0].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[0].Attributes.Add("rowspan", "2");
                tcHeader[0].Text = "序号";
                tcHeader[0].Font.Bold = false;
                tcHeader[0].Style["text-align"] = "center";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[1].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[1].Attributes.Add("rowspan", "2");
                tcHeader[1].Text = "科室";
                tcHeader[1].Font.Bold = false;
                tcHeader[1].Style["text-align"] = "center";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[2].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[2].Attributes.Add("rowspan", "2");
                tcHeader[2].Text = "教师姓名";
                tcHeader[2].Font.Bold = false;
                tcHeader[2].Style["text-align"] = "center";


              

                tcHeader.Add(new TableHeaderCell());
                tcHeader[3].Attributes.Add("colspan", "7"); //跨Row
                tcHeader[3].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[3].Text = "发送短信数";
                tcHeader[3].Font.Bold = false;
                tcHeader[3].Style["text-align"] = "center";


                tcHeader.Add(new TableHeaderCell());
                tcHeader[4].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[4].Attributes.Add("rowspan", "2");
                tcHeader[4].Text = "总计</th></tr><tr>";
                tcHeader[4].Font.Bold = false;
                tcHeader[4].Style["text-align"] = "center";

 

                tcHeader.Add(new TableHeaderCell());
                tcHeader[5].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[5].Text = "留&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;言";
                tcHeader[5].Font.Bold = false;
                tcHeader[5].Style["text-align"] = "center";

 

                tcHeader.Add(new TableHeaderCell());
                tcHeader[6].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[6].Text = "通&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;知";
                tcHeader[6].Font.Bold = false;
                tcHeader[6].Style["text-align"] = "center";

 

                tcHeader.Add(new TableHeaderCell());
                tcHeader[7].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[7].Text = "成&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;绩";
                tcHeader[7].Font.Bold = false;
                tcHeader[7].Style["text-align"] = "center";

 

                tcHeader.Add(new TableHeaderCell());
                tcHeader[8].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[8].Text = "评&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;语";
                tcHeader[8].Font.Bold = false;
                tcHeader[8].Style["text-align"] = "center";

 

                tcHeader.Add(new TableHeaderCell());
                tcHeader[9].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[9].Text = "报安考勤";
                tcHeader[9].Font.Bold = false;
                tcHeader[9].Style["text-align"] = "center";

 

                tcHeader.Add(new TableHeaderCell());
                tcHeader[10].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[10].Text = "家庭作业";
                tcHeader[10].Font.Bold = false;
                tcHeader[10].Style["text-align"] = "center";

 

                tcHeader.Add(new TableHeaderCell());
                tcHeader[11].Attributes.Add("bgcolor", "#E8E8E8");
                tcHeader[11].Text = "自由短信";
                tcHeader[11].Font.Bold = false;
                tcHeader[11].Style["text-align"] = "center";

              
           
            }
        }

 

        //第一种方法

        private string GetGradeClassCellHead(string cellTitle)
        {
            string html = string.Format("<table align='center'><tr><td align='center'>{0}</td></tr></table>", cellTitle);
            return html;
        }
        private string GetMegCell()
        {
            string css = "align=/"center/" scope=/"col/" style=/"font-weight:normal;font-style:normal;text-decoration:none;border-right:1px solid #D4D0C8;border-collapse:collapse;/"";
            string html = "<table align=/"center/" cellspacing=/"0/" cellpadding=/"2/" rules=/"all/" border=/"0/" style=/"border-width:0px;border-style:solid;width:100%;border-collapse:collapse;/"><tr>" +
                "<td colspan=/"7/" align=/"center/" scope=/"col/" " +
                "style=/"font-weight:normal;font-style:normal;text-decoration:none;border-bottom:1px solid #D4D0C8;border-collapse:collapse;/">发送短信数</td></tr>" +
                "<tr><td " + css + ">留&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;言</td><td " + css + ">通&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;知</td><td " + css + ">成&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;绩</td><td " + css + ">评&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;语</td><td " + css + ">报安考勤</td><td " + css + ">家庭作业</td>" +
                "<td align=/"center/" scope=/"col/" style=/"font-weight:normal;font-style:normal;text-decoration:none;/">自由短信</td></tr></table>";
            return html;
        }

 

 

 

 

   //单元格要合并的行数
        public int intRowSpan = 1;
        //开始合并的索引值
        public int intIndex = 0;
        //记录第一列相等的条件
        private string strText = "";

        protected void gvStudent_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex == 0)
            {
                strText = e.Row.Cells[1].Text;
            }
            if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != 0)
            {
                if (e.Row.Cells[1].Text == strText)
                {
                    intRowSpan++;
                    gvStudent.Rows[intIndex].Cells[1].RowSpan = intRowSpan;
                    e.Row.Cells[1].Visible = false;
                }
                else
                {
                    intRowSpan = 1;
                    intIndex = e.Row.RowIndex;
                }
                strText = e.Row.Cells[1].Text;
            }
        }

       

第一种方法,比较麻烦,而且在填充数据后,表格显示不规则,所以第二种是首选,注意的地方就是不管你表头有几列,都跟table中的行和列之间的合并是一样的,理清楚后,就很容易了。

 

DataGrid:

       在页面上 是 OnItemCreated="DataGrid1_ItemCreated" 属性;

     protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.Header:
                    TableCellCollection tcHeader = e.Item.Cells;
                    tcHeader.Clear();
                    if (ScoreType == "1")
                    {
                        tcHeader.Add(new TableHeaderCell());
                        tcHeader[0].Attributes.Add("bgcolor", "#E8E8E8");
                        tcHeader[0].Text = "学期";
                        tcHeader[0].Font.Bold = false;
                        tcHeader[0].Style["text-align"] = "center";
                        tcHeader.Add(new TableHeaderCell());
                        tcHeader[1].Attributes.Add("bgcolor", "#E8E8E8");
                        tcHeader[1].Text = "考试安排";
                        tcHeader[1].Font.Bold = false;
                        tcHeader[1].Style["text-align"] = "center";
                        tcHeader.Add(new TableHeaderCell());
                        tcHeader[2].Attributes.Add("bgcolor", "#E8E8E8");
                        tcHeader[2].Text = CourseName;
                        tcHeader[2].Font.Bold = false;
                        tcHeader[2].Style["text-align"] = "center";
                    }
                    else
                    {
                        tcHeader.Add(new TableHeaderCell());
                        tcHeader[0].Attributes.Add("bgcolor", "#E8E8E8");
                        tcHeader[0].Attributes.Add("rowspan", "2");
                        tcHeader[0].Text = "学期";
                        tcHeader[0].Font.Bold = false;
                        tcHeader[0].Style["text-align"] = "center";
                        tcHeader.Add(new TableHeaderCell());
                        tcHeader[1].Attributes.Add("bgcolor", "#E8E8E8");
                        tcHeader[1].Attributes.Add("rowspan", "2");
                        tcHeader[1].Text = "考试安排";
                        tcHeader[1].Font.Bold = false;
                        tcHeader[1].Style["text-align"] = "center";
                        tcHeader.Add(new TableHeaderCell());
                        tcHeader[2].Attributes.Add("colspan", "3"); //跨Row
                        tcHeader[2].Attributes.Add("bgcolor", "#E8E8E8");
                        tcHeader[2].Text = CourseName + "</th></tr><tr>";
                        tcHeader[2].Font.Bold = false;
                        tcHeader[2].Style["text-align"] = "center";

                        tcHeader.Add(new TableHeaderCell());
                        tcHeader[3].Attributes.Add("bgcolor", "#E8E8E8");
                        tcHeader[3].Text = "成绩";
                        tcHeader[3].Font.Bold = false;
                        tcHeader[3].Style["text-align"] = "center";

                        tcHeader.Add(new TableHeaderCell());
                        tcHeader[4].Attributes.Add("bgcolor", "#E8E8E8");
                        tcHeader[4].Text = "个人班级比";
                        tcHeader[4].Font.Bold = false;
                        tcHeader[4].Style["text-align"] = "center";

                        tcHeader.Add(new TableHeaderCell());
                        tcHeader[5].Attributes.Add("bgcolor", "#E8E8E8");
                        tcHeader[5].Text = "个人年级比";
                        tcHeader[5].Font.Bold = false;
                        tcHeader[5].Style["text-align"] = "center";
                    }
                    break;
            }
        }

  合并方法是在绑定数据之后:

  private void initDataGrid()
        {
           .........

                DataTable table = analysis.CourseProgressTrack(StudentID, ExamTypeID, SchoolID, ClassID, CourseCode, ScoreType, RepeatCourse);
                DataGrid1.DataSource = table;
                DataGrid1.DataBind();
                GroupRows(DataGrid1, 0);
            }
        }

 

 

 /// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="GridView1"></param>
        /// <param name="cellNum"></param>
        public static void GroupRows(System.Web.UI.WebControls.DataGrid GridView1, int cellNum)
        {
            int i = 0, rowSpanNum = 1;
            while (i < GridView1.Items.Count - 1)
            {
                DataGridItem gvr = GridView1.Items[i];
                for (++i; i < GridView1.Items.Count; i++)
                {
                    DataGridItem gvrNext = GridView1.Items[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.Items.Count - 1)
                    {
                        gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    }
                }
            }
        }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值