黃忠成

風雪之閣- i live,so i writing
文章數 - 133, 回應數 - 125, 引用數 - 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();
    }

     

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


    DotBlogs Tags: ASP.NET

    posted on 2008/5/7 21:07 | 我要推薦 | 閱讀數 : 3318 | 文章分類 [ 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 | 許耀彰

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

    你好,
    我使用大家普遍使用的程式碼

    Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PreRender

    Dim mergeColumns As Integer() = {1}
    '要合併的欄位(合併Group1與Group2)

    For Each mergeColumn As Integer In mergeColumns
    Dim i As Integer = 1
    For Each wkItem As GridViewRow In GridView1.Rows
    If wkItem.RowIndex <> 0 Then
    If wkItem.Cells(mergeColumn).Text.Trim() = GridView1.Rows((wkItem.RowIndex - i)).Cells(mergeColumn).Text.Trim() Then
    GridView1.Rows((wkItem.RowIndex - i)).Cells(mergeColumn).RowSpan += 1
    wkItem.Cells(mergeColumn).Visible = False
    i = i + 1
    Else
    GridView1.Rows((wkItem.RowIndex)).Cells(mergeColumn).RowSpan += 1
    i = 1
    End If
    Else
    wkItem.Cells(mergeColumn).RowSpan = 1
    End If
    Next
    Next

    為何欄位都是合併成一個呢?
    我有trace code
    發現wkItem.Cells(mergeColumn).Text 都式空白""
    為何會這樣呢?
    還請黃'r 代為釋疑
    謝謝
    2009/12/29 上午 11:33 | ken

    回應

    標題
    姓名
    電子郵件 (將不會被顯示)
    個人網頁
    內容 
      登入後使用進階評論  
    Please add 1 and 7 and type the answer here:

    Powered by: