19
2013
12

Asp.Net下GridView合并TemplateField模板列和BoundField绑定列方法

BoundField绑定列的合并

        /// <summary>
        /// 合并BoundField绑定列
        /// </summary>
        /// <param name="gv">需要合并GridView</param>
        /// <param name="cellNum">第几列</param>
        public void GroupRows(GridView gv, int cellNum)
        {
            int i = 0, rowSpanNum = 1;
            while (i < gv.Rows.Count - 1)
            {
                GridViewRow gvr = gv.Rows[i];
                for (++i; i < gv.Rows.Count; i++)
                {
                    GridViewRow gvrNext = gv.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 == gv.Rows.Count - 1)
                    {
                        gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    }
                }
            }
        }

TemplateField模板列的合并

        /// <summary>
        /// 合并TemplateField模板列(合并的列要用Label控件)
        /// </summary>
        /// <param name="gvw">需要合并的GridView</param>
        /// <param name="sCol">要合并的列(从0开始)</param>
        /// <param name="controlName">控件名称</param>
        public void MergeRows(GridView gvw, int col, string controlName)
        {
            for (int rowIndex = gvw.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            {
                GridViewRow row = gvw.Rows[rowIndex];

                GridViewRow previousRow = gvw.Rows[rowIndex + 1];

                Label row_lbl = row.Cells[col].FindControl(controlName) as Label;
                Label previousRow_lbl = previousRow.Cells[col].FindControl(controlName) as Label;

                if (row_lbl != null && previousRow_lbl != null)
                {
                    if (row_lbl.Text == previousRow_lbl.Text)
                    {
                        row.Cells[col].RowSpan = previousRow.Cells[col].RowSpan < 1 ? 2 : previousRow.Cells[col].RowSpan + 1;

                        previousRow.Cells[col].Visible = false;
                    }
                }
            }
        }

« 上一篇下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。