Repeat header row after each row dynamically

How can I repeat the header row after each row of gridview?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You might add code to the rowdatabound-event of your grid:

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 * 2 + 1, row);            
    }
}

In this example, I have removed the gridview header altogether as you write your own in code behind.

Method 2

You can’t without re-writing the control render method. How about using a repeater and putting it in the item separator element too?

Method 3

Well, it is way easier to either put the markup for the header also in the item template. Overriding the Render method for something like this is probably overkill.

But if there is a reason to do this, it is at least missing from your question.

Method 4

If you need the header row markup in every single item row, why not just include the markup that is currently in your header into the ItemTemplate …?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x