Set HTML header items programmatically in ASP.NET 2.0 Adding Rows and Cells Dynamically to a Table Control
Oct 06

Just override the OnDataBound method and add the extra rows. In this example, the number of rows is always the same as the PageSize property and still keeping the footer row at the bottom.

protected override void OnDataBound(EventArgs e)
{
GridViewRow gvRow = null;

for (int rows = this.Rows.Count; rows < this.PageSize; rows++)
{
gvRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);

for (int columns = 0; columns < this.Columns.Count; columns++)
{
gvRow.Controls.Add(new TableCell());
}

//Inserts the rows right above the footer row.
//Remove the "- 1" if you are not using a footer.
this.Controls[0].Controls.AddAt(this.Controls[0].Controls.Count - 1, gvRow);
}
}

Leave a Reply