隨筆-200  評論-148  文章-15  trackbacks-0
          ///     
              ///   合并GridView列中相同的行  
              ///     
              ///   GridView對象  
              ///   需要合并的列  
              public static void GroupRows(GridView GridView1, int cellNum)
              {
                  int i = 0, 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;
                          }
                      }
                  }
              }

              ///     
              ///   根據條件列合并GridView列中相同的行  
              ///     
              ///   GridView對象  
              ///   需要合并的列
              ///   ///   條件列(根據某條件列還合并)
              public static void GroupRows(GridView GridView1, int cellNum, int cellNum2)
              {
                  int i = 0, 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 + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].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;
                          }
                      }
                  }
              }

           

          第二個方法只是在第一個方法的修改了一點點, 在判斷單元格的時候,加上了一個條件列:

          if (gvr.Cells[cellNum].Text + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].Text)。當然,第二個方法還可以擴展,根據多個列的條件來合并。
          【本文地址:http://www.opent.cn/a/2009/2/6/861.shtml
          posted on 2010-10-14 12:02 無聲 閱讀(243) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 麻栗坡县| 洮南市| 安义县| 元江| 河池市| 邳州市| 襄樊市| 旬阳县| 常州市| 洪泽县| 日照市| 玛纳斯县| 绥滨县| 永康市| 新巴尔虎左旗| 建水县| 白河县| 原阳县| 五指山市| 万载县| 汾阳市| 建水县| 池州市| 高雄市| 资中县| 梅河口市| 堆龙德庆县| 阿鲁科尔沁旗| 安国市| 奉节县| 保定市| 苏州市| 哈密市| 宿迁市| 巫山县| 周至县| 称多县| 阿拉善盟| 嵊泗县| 祁门县| 虎林市|