GridView合并某一列相同的单元格(MS)

 一次只合并一列

        #region  合并gridview中某一列相同的单元格
        public static void CellTogether(int cellIndex, GridView gvTemp)
        {
            int i = 0;
            int rowSpanNum = 1;

            while (i < gvTemp.Rows.Count - 1)
            {
                GridViewRow gvr = gvTemp.Rows[i];
                for (++i; i < gvTemp.Rows.Count; i++)
                {
                    GridViewRow gvrNext = gvTemp.Rows[i];
                    if (gvr.Cells[cellIndex].Text == gvrNext.Cells[cellIndex].Text)
                    {
                        // 前1つ列の内容が一致かどうか
                        if ((cellIndex >= 1) &&
                            (gvr.Cells[cellIndex - 1].Text != gvrNext.Cells[cellIndex - 1].Text))
                        {
                            gvr.Cells[cellIndex].RowSpan = rowSpanNum;
                            rowSpanNum = 1;
                            break;
                        }
                        else
                        {
                            gvrNext.Cells[cellIndex].Visible = false;
                            rowSpanNum++;
                        }
                    }
                    else
                    {
                        gvr.Cells[cellIndex].RowSpan = rowSpanNum;
                        rowSpanNum = 1;
                        break;
                    }
                    if (i == gvTemp.Rows.Count - 1)
                    {
                        gvr.Cells[cellIndex].RowSpan = rowSpanNum;
                    }
                }
            }
        }

        #endregion

使用方法

在GRIDVIEW绑定后调用

CellTogetherArry(列数字, GridView1);

 

 

一次可以合并多列(数组)

        public static void CellTogetherArry(int[] cellIndex, GridView gvTemp)
        {
            for (int x = 0; x < cellIndex.Length; x++)
            {
                int i = 0;
                int rowSpanNum = 1;
                while (i < gvTemp.Rows.Count - 1)
                {
                    GridViewRow gvr = gvTemp.Rows[i];
                    for (++i; i < gvTemp.Rows.Count; i++)
                    {
                        GridViewRow gvrNext = gvTemp.Rows[i];
                        if (gvr.Cells[cellIndex[x]].Text == gvrNext.Cells[cellIndex[x]].Text)
                        {
                            // 前1つ列の内容が一致かどうか
                            if ((cellIndex[x] >= 1) &&
                                (gvr.Cells[cellIndex[x] - 1].Text != gvrNext.Cells[cellIndex[x] - 1].Text))
                            {
                                gvr.Cells[cellIndex[x]].RowSpan = rowSpanNum;
                                rowSpanNum = 1;
                                break;
                            }
                            else
                            {
                                gvrNext.Cells[cellIndex[x]].Visible = false;
                                rowSpanNum++;
                            }
                        }
                        else
                        {
                            gvr.Cells[cellIndex[x]].RowSpan = rowSpanNum;
                            rowSpanNum = 1;
                            break;
                        }
                        if (i == gvTemp.Rows.Count - 1)
                        {
                            gvr.Cells[cellIndex[x]].RowSpan = rowSpanNum;
                        }
                    }
                }
            }
        }

 

使用方法

int[] arr=new int[]{1,2};

在GRIDVIEW绑定后调用

CellTogetherArry(arr, GridView1);

你可能感兴趣的:(GridView)