日期:2014-05-17  浏览次数:20483 次

GridView绑定DataTable后怎样合并连续相同的列
如题,我试了两种方法了都不行。
绑定代码

            gvNet.DataSource = dt;
            gvNet.DataBind();

失败的方法1:

    public void GroupCol(int cols, int sRow, int eRow)
    {
        if (gvNet.Rows.Count < 1 || cols > gvNet.Columns.Count - 1)
        {
            return;
        }
        TableCell oldTc = gvNet.Rows[sRow].Cells[cols];
        for (int i = 1; i < eRow - sRow; i++)
        {
            TableCell tc = gvNet.Rows[sRow + i].Cells[cols];
            tc.Visible = false;
            if (oldTc.RowSpan == 0)
            {
                oldTc.RowSpan = 1;
            }
            oldTc.RowSpan++;
            oldTc.VerticalAlign = VerticalAlign.Middle;
        }
    }

失败的方法2:

    public static void MergeSameRows(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
    &