分享

C# web GridView 单元格合并

 悟静 2012-05-06

http://www.cnblogs.com/linbaoji/archive/2009/08/24/1553166.html感谢牛人
        //   <summary>  
        ///   合并GridView列中相同的行  :证实可用
        ///   </summary>  
        ///   <param   name="gridView">GridView对象</param>  
        ///   <param   name="cellNum">需要合并的列</param>  
        public void GroupRows(GridView gridView, int cellNum)
        {
            int i = 0, rowSpanNum = 1;
            while (i < gridView.Rows.Count - 1)
            {
                GridViewRow gvr = gridView.Rows[i];
                for (++i; i < gridView.Rows.Count; i++)
                {
                    GridViewRow gvrNext = gridView.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 == gridView.Rows.Count - 1)
                    {
                        gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    }
                }
            }
        }

       private void BindData()//GridView 数据绑定
        {         
            this.gridViewExample.DataSource = getDataTable();
            this.gridViewExample.DataBind();
            GroupRows(
gridViewExample, 0);//合并第一列
            GroupRows(
gridViewExample, 1); //合并第二列        
        }

        private DataTable getDataTable()//数据绑定源
        {
            DataTable dt = new DataTable("gvExample");


            DataColumn dc = new DataColumn();
            dc.ColumnName = "userid";
            dt.Columns.Add(dc);
            dc = new DataColumn();
            dc.ColumnName = "username";
            dt.Columns.Add(dc);
            dc = new DataColumn();
            dc.ColumnName = "userlove";
            dt.Columns.Add(dc);


            DataRow dr = dt.NewRow();
            dr["userid"] = "1";
            dr["username"] = "vincent";
            dr["userlove"] = "swim";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["userid"] = "1";
            dr["username"] = "vincent";
            dr["userlove"] = "read";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["userid"] = "1";
            dr["username"] = "vincent";
            dr["userlove"] = "history";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["userid"] = "2";
            dr["username"] = "green";
            dr["userlove"] = "coding";
            dt.Rows.Add(dr);
            return dt;
        }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多