黃忠成

風雪之閣- i live,so i writing
文章數 - 101, 回應數 - 53, 引用數 - 0


關於我:



黃忠成

  • 資深.NET 技術顧問
  • Run! PC 雜誌專欄作者
  • 程序員雜誌文章作者
  • PC Magazine 雜誌專欄作者
  • MSDN 專欄作者
  • MSDN 特約專屬講師
  • Microsoft .NET專屬講師
  • 台灣微軟特約技術顧問
  • 台灣微軟最有價值專家


  • 批評,指教,鼓勵, 請 寫信給我
    轉載文章請使用連結模式,
    請勿整篇Copy! 謝謝!


    我所提供的教育訓練:

    Windows Forms
    ASP.NET 2.0
    如有課程需要,請與我聯絡!

  • 我的著作:

  • 文章標籤

    全部標籤

    每月文章

    文章分類

    MSDN ASP.NET GridView 範例集 Group Row 部份程式碼修正

      可能因為排版緣故,導致刊登於MSDN上的ASP.NET GridView 範例集中Group Row的部份程式碼有錯誤,

     特於此列出正確的程式碼(主要是泛型List的泛型參數問題,由於使用了<,> 符號,可能於排版時漏掉了).

     

     

    Group Row 程式碼

    private void PrepareGroup()
    {
          int lastSupID = -1;
          GridViewRow currentRow = null;
          List<GridViewRow>  tempModifyRows = new List<GridViewRow>();
          foreach (GridViewRow row in GridView1.Rows)
          {           
             if (row.RowType == DataControlRowType.DataRow)
             {
                if (currentRow == null)
                {
                    currentRow = row;
                    int.TryParse(row.Cells[2].Text, out lastSupID);
                    continue;
                }

                int currSupID = -1;
                if (int.TryParse(row.Cells[2].Text, out currSupID))
                {
                   if (lastSupID != currSupID)
                   {
                      currentRow.Cells[2].Attributes["rowspan"] = (tempModifyRows.Count+1).ToString();
                      currentRow.Cells[2].Attributes["valign"] = "center";                       
                      foreach (GridViewRow row2 in tempModifyRows)
                         row2.Cells.RemoveAt(2);
                      lastSupID = currSupID;
                      tempModifyRows.Clear();
                      currentRow = row;
                      lastSupID = currSupID;
                   }
                   else
                      tempModifyRows.Add(row);
            }                   
       }
    }

      if (tempModifyRows.Count > 0)
      {
           currentRow.Cells[2].Attributes["rowspan"] = (tempModifyRows.Count + 1).ToString();
           currentRow.Cells[2].Attributes["valign"] = "center";
           foreach (GridViewRow row2 in tempModifyRows)
                row2.Cells.RemoveAt(2);
      }
    }

    protected void GridView1_PreRender(object sender, EventArgs e)
    {
        PrepareGroup();
    }

     

    抱歉造成各位讀者的困擾了 ^_^


    posted on 2008/5/7 21:07 | 我要推薦 | 閱讀數 : 812 | 文章分類 [ ASP.NET ] 訂閱

    Feedback

    # re: MSDN ASP.NET GridView 範例集 Group Row 部份程式碼修正 回覆

    請教前輩
    這個是否有vb版的版本呢?
    謝謝
    2008/6/13 下午 01:56 | 許耀彰

    # re: MSDN ASP.NET GridView 範例集 Group Row 部份程式碼修正 回覆

    網路上有許多C# 轉VB.NET的工具,應能協助解決你的問題.
    2008/6/18 上午 09:57 | code6421

    # re: MSDN ASP.NET GridView 範例集 Group Row 部份程式碼修正 回覆

    好的 我再試試
    謝謝
    2008/7/2 下午 10:31 | 許耀彰

    回應

    標題
    姓名
    電子郵件
    個人網頁
    內容 
      登入後使用進階評論  
    Please add 5 and 2 and type the answer here:

    Powered by: