2011-12-09 38 views
0

在asp.net中,我使用gridview來顯示數據。在gridview中,我一次顯示300條記錄。現在我想在GridView中的每30行之後顯示標題模板嗎?你可以幫我嗎。謝謝。 我這樣寫在gridview_rowdatabound中。如何在gridivew中重複標題模板?

 GridViewRow row = new GridViewRow(e.Row.RowIndex, 0, DataControlRowType.Header, DataControlRowState.Normal); 
     TableCell cell = new TableCell(); 
     cell.Controls.Add(new Label { Text = "Header" }); //as needed    
     row.Cells.Add(cell); 
     cell = new TableCell(); 
     cell.Controls.Add(new Label { Text = "Name" }); //as needed    
     row.Cells.Add(cell); 

     cell = new TableCell(); 
     cell.Controls.Add(new Label { Text = "add" }); //as needed    
     row.Cells.Add(cell); 

     cell = new TableCell(); 
     cell.Controls.Add(new Label { Text = "Nice" }); //as needed    
     row.Cells.Add(cell); 

     Table tbl = (e.Row.Parent as Table); 
     tbl.Controls.AddAt(e.Row.RowIndex * 3 + 1, row); 

當我把tbl.Controls.AddAt(e.Row.RowIndex * 2 + 1,行);當我像這樣放置2個像3個,4個時,它正在工作,那麼它就是連接錯誤。

謝謝所有我解決這個問題:

if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
     if (e.Row.RowIndex == 0) 
     { 
      ViewState["i"] = e.Row.RowIndex; 
     } 

     if (Convert.ToInt32(ViewState["i"].ToString()) == e.Row.RowIndex) 
     { 

      GridViewRow row = new GridViewRow(e.Row.RowIndex, 0, DataControlRowType.Header, DataControlRowState.Normal); 
      TableCell cell = new TableCell(); 
      cell.BackColor = System.Drawing.Color.BurlyWood; 
      cell.Controls.Add(new Label { Text = "header" }); //as needed    
      row.Cells.Add(cell); 

      cell = new TableCell(); 
      cell.BackColor = System.Drawing.Color.BurlyWood; 
      cell.Controls.Add(new Label { Text = "Name" }); //as needed    
      row.Cells.Add(cell); 

      cell = new TableCell(); 
      cell.BackColor = System.Drawing.Color.BurlyWood; 
      cell.Controls.Add(new Label { Text = "add" }); //as needed    
      row.Cells.Add(cell); 

      cell = new TableCell(); 
      cell.BackColor = System.Drawing.Color.BurlyWood; 
      cell.Controls.Add(new Label { Text = "Nice" }); //as needed    
      row.Cells.Add(cell); 
      int cnt = Convert.ToInt32(ViewState["i"].ToString()); 

      Table tbl = (e.Row.Parent as Table); 

      //tbl.Controls.AddAt(e.Row.RowIndex * cnt , row); 
      tbl.Controls.AddAt(e.Row.RowIndex, row);    

      cnt = cnt + 4; 
      ViewState["i"] = cnt.ToString(); 

     } 

    } 
+0

謝謝你,答覆,我解決這個問題。 –

+0

我把e.row.index放在integr變量中,並檢查條件if(e.row.rowindex == i)然後顯示標題,如果條件我給我賦值,就像i = i + 30,之前我寫了一個條件來確定價值,請檢查我正在更新我的問題,因爲我正在放置代碼。 –

回答

1

嘗試這種方式

  1. 從你的aspx頁面刪除你的你的GridView的頭

  2. 現在寫的RowDataBound方法

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
        if (e.Row.RowType == DataControlRowType.DataRow) 
        { 
         GridViewRow row = new GridViewRow(e.Row.RowIndex, 0, DataControlRowType.Header, DataControlRowState.Normal); 
    
         TableCell cell = new TableCell(); 
         cell.Controls.Add(new Label { Text = "Header" }); //as needed 
    
         row.Cells.Add(cell); 
    
         Table tbl = (e.Row.Parent as Table); 
    
         tbl.Controls.AddAt(e.Row.RowIndex * 29 + 1, row);    
        } 
    } 
    

Repeat Header row after few rows dynamically

+0

Anadnd先生事實上它拋出的錯誤如下:指定的參數超出了有效值的範圍。 參數名稱:索引 –

+0

請在此處添加所需的內容,如cell.Controls.Add(new Label {Text =「Name」}); cell.Controls.Add(new Label {Text =「LastName」}); – AnandMohanAwasthi

+0

謝謝,我解決了這個問題 –

0

以下解決方案使用的GridView的實際報頭,因此不依靠手動添加一個標籤爲每個列:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowIndex + 1) % 30 == 0) 
    { 
     GridViewRow row = new GridViewRow(e.Row.RowIndex, 0, DataControlRowType.Header, DataControlRowState.Normal); 

      for(int i = 0; i < gv.Columns.Count; i++) 
      { 
       if (gv.Columns[i].Visible) 
       { 
        TableCell objTC = new TableCell(); 
        objTC.Font.Bold = true; 
        objTC.Text = gv.Columns[i].HeaderText; 
        row.Cells.Add(objTC); 
       } 
      } 

     Table tbl = (e.Row.Parent as Table); 

     // add row above the current row so that sequencing isn't affected 
     tbl.Controls.AddAt(e.Row.RowIndex - 1, row); 
    } 
}