How To Force Force x number of rows in a GridView

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);
       }
   }
Reactions

Post a Comment

0 Comments

Close Menu