zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    Windows环境下安装Hadoop+Hive的使用案例
    基于hadoop的离线分析大数据工具Hive的架构图
    springboot集成elasticsearch
    亿级流量场景下,大型架构设计实现【全文检索高级搜索---ElasticSearch篇】-- 中
    海量数据,大数据处理技术--分布式数据库【Hbase】
    亿级流量场景下,大型架构设计实现【全文检索高级搜索---ElasticSearch篇】-- 上
    zookeeper安装以及遇到的一些坑
    亿级流量场景下,大型架构设计实现【2】---storm篇
    用到UdpClient的一点经验
    随笔
  • 原文地址:https://www.cnblogs.com/Hdsome/p/1261959.html
Copyright © 2011-2022 走看看