隨筆-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)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 九寨沟县| 晴隆县| 阿坝县| 静乐县| 长治市| 柳江县| 建昌县| 老河口市| 南平市| 灵川县| 吉木乃县| 乾安县| 沧州市| 肇东市| 常熟市| 无锡市| 双江| 富蕴县| 大姚县| 巩义市| 德兴市| 司法| 大新县| 阳谷县| 河津市| 和平县| 曲周县| 襄城县| 二手房| 苍溪县| 台安县| 富源县| 深州市| 胶州市| 茂名市| 遂宁市| 巫山县| 屯门区| 廊坊市| 青海省| 凉山|