Force x number of rows in a GridView

Mar 6, 2006

Today I had to find a way to force x number of rows on a GridView. The problem is that if the GridView.PageSize is larger than the number of rows you are data binding, the GridView itself gets smaller in the height. You often see it when paging through the pages of a grid. The last page is always shorter than the rest, because there are fewer rows. That is usually not a issue, but I had to keep our designers happy.

It is actually pretty simple in a class that inherits from System.Web.UI.WebControls.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);
 }
}

* $4.95/month BlogEngine.net Hosting – Click Here!

Comments (2) -

 Mike J
Mike J
11/28/2006 3:30:58 PM #

I was wondering if you have any pointers to reference on how I would go about extending the GridView class to override the method above.  Do I need to create a class that is named differently from a GridView and extends it, and add this override method?  Does that all go in it's own file?

Rob Bruce
Rob Bruce United Kingdom
4/9/2008 11:35:27 AM #

Just a few improvments: doesn't show hidden columns, and sets row states as alternative/normal accordinly.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;

namespace Control
{
  [ToolboxData("<{0}:FixedGridView runat=\"server\"></{0}:EncryptedTextBox>")]
  public class FixedGridView : GridView
  {

    protected override void OnDataBound(System.EventArgs e)
    {
      GridViewRow gvRow = null;
      for (int iRows = this.Rows.Count; iRows <= (this.PageSize - 1); iRows++) {
        gvRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
        if ((iRows % 2) == 1)
        {
          gvRow.RowState = DataControlRowState.Alternate;
        }
        for (int iColumns = 0; iColumns <= (this.Columns.Count - 1); iColumns++) {
          if (this.Columns(iColumns).Visible)
          {
            TableCell tblCell = new TableCell();
            tblCell.Text = "&nbsp;";
            gvRow.Controls.Add(tblCell);
          }
        }
        this.Controls(0).Controls.AddAt(this.Controls(0).Controls.Count - 1, gvRow);
      }
    }
  }
}

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.