闲坐敲棋

有约不来过夜半,闲敲棋子落灯花

导航

GridView 合并单元格

Posted on 2009-03-29 16:22  闲坐敲棋  阅读(646)  评论(0编辑  收藏  举报

 /// <summary>
    /// 合并单元格
    /// </summary>
    /// <param name="gridView">要合并单元格的表单</param>
    /// <param name="ArryJudgeCell">判断的列</param>
    /// <param name="ArryCellNum">要合并的列</param>
    public static void DoUniteGroupRowsGrid(GridView gridView, int[] ArryJudgeCell, int[] ArryCellNum)
    {
        int i = 0, rowSpanNum = 1;

        bool flagSame = true;

        while (i < gridView.Rows.Count - 1)
        {
            // 得到当前行
            GridViewRow gvrThis = gridView.Rows[i];

            for (++i; i < gridView.Rows.Count; i++)
            {
                GridViewRow gvrNext = gridView.Rows[i];

                for (int j = 0; j < ArryJudgeCell.Length; j++)
                {
                    flagSame = true;

                    if (gvrThis.Cells[ArryJudgeCell[j]].Text != gvrNext.Cells[ArryJudgeCell[j]].Text)
                    {
                        flagSame = false;

                        break;
                    }
                }

                if (flagSame)
                {
                    for (int k = 0; k < ArryCellNum.Length; k++)
                    {
                        gvrNext.Cells[ArryCellNum[k]].Visible = false;
                    }

                    rowSpanNum++;
                }
                else
                {
                    for (int k = 0; k < ArryCellNum.Length; k++)
                    {
                        gvrThis.Cells[ArryCellNum[k]].RowSpan = rowSpanNum;
                    }
                    rowSpanNum = 1;
                    break;
                }

                if (i == gridView.Rows.Count - 1)
                {
                    for (int k = 0; k < ArryCellNum.Length; k++)
                    {
                        gvrThis.Cells[ArryCellNum[k]].RowSpan = rowSpanNum;
                    }
                }
            }
        }
    }