对gridview的小改动

///


    /// 合并某列内容相同的行
    ///

    ///
    ///
    public static void GroupRows(GridView GridView1, int cellNum)
    {
        int i = 0;
        int 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;
                }
            }
        }
    }

protected void gvMain_DataBound(object sender, EventArgs e)
    {
        GroupRows(this.gvMain, 1);

    }

//加合计行

protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.Footer && e.Row.RowType != DataControlRowType.Header)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;

            iNumber = Convert.ToInt32(drv[4]);
            totalNumber += iNumber;
        }
        if (e.Row.RowType == DataControlRowType.Footer) 
        {
            e.Row.Cells[4].Text = string.Format("{0}", totalNumber);
            e.Row.Cells[1].Text = "合计";
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[1].ColumnSpan = 2;
            e.Row.Height = 32;
        }
    }

欢迎拍砖,欢迎转载,欢迎关注,欢迎联系,就是各种欢迎
原文地址:https://www.cnblogs.com/EddyPeng/p/1225883.html