Showing ASP.NET GridView Headers and Footers with Empty Datasource

Monday, April 12th, 2010 | Stuff

When you try to bind the standard ASP.NET GridView to an empty datasource - it renders nothing (or your EmptyDataTemplate). There are several ways to make it render you headers/footers, including:

  • Inheriting the GridView class and overriding CreateChildControls().  You then need to worry about overriding HeaderRow and FooterRow, and you basically reimplement the GridView. (Most elegant solution)
  • Bind it to something that’s not empty (maybe something that says “No data” in one of the rows.
  • Cheap hack: Below

The cheap hack solution is to check if your datasource is empty before you bind it.  If it is, bind it to some dummy data (just one row will do), and after DataBind(), loop through the Rows collection and for each row which has a RowType of DataRow, set the Visible property to false.

Yes, shame, but it’s fast and works without mucking around.

~

No comments yet.

Leave a comment