如何在gridview中重复标题模板?

时间:2011-12-09 08:58:24

标签: asp.net

在asp.net中,我使用gridview来显示数据。在gridview中,我一次显示300条记录。现在我想在gridview中每隔30行显示一个标题模板吗? 我在gridview_rowdatabound中这样写。

     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,row);当我像这样放置2个像4个这样的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();
        }
    } 

3 个答案:

答案 0 :(得分: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);            
        }
    }   
    
  3. Repeat Header row after few rows dynamically

答案 1 :(得分:0)

请你看一下:GridView Examples for Paging,谢谢你的时间。

答案 2 :(得分: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);
    }
}