Displaying GridView When No Data Exists

Posted by: Clarity Blogs: ASP.NET, on 28 Feb 2006 | View original | Bookmarked: 0 time(s)

One thing thats midly annoying to me about the new gridview control is that it doesn't display the header row when there is no data.  I would like to just have the header displayed like the original table with a single row saying something like "No records found".  The grid has a property for emptydata text which doesn't look that great in my opinion.  I have seen some people use the <EmptyDataTemplate> property to create a new table in there which isn't the cleanest solution.  One way to get the gridview to display how you want is to create a custom control that inherts from gidview.  Then you can override the CreateChildControls method to create a the gridview with the all the headers and a blank row with one cell and a text message.  Here's how to do it:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

public class EmptyGridView : GridView
{
    #region Properties

    /// <summary>
    /// Enable or Disable generating an empty table if no data rows in source
    /// 
</summary>
    [
    Description("Enable or disable generating an empty table with headers if no data rows in source"),
    Category("Misc"),
    DefaultValue("true"),
    ]
    public bool ShowEmptyTable
    {
        get
        {
            object o = ViewState["ShowEmptyTable"];
            return (o != null ? (bool)o : true);
        }
        set
        {
            ViewState["ShowEmptyTable"] = value;
        }
    }
    
    /// 
<summary>
    /// Get or Set Text to display in empty data row
    /// 
</summary>
    [
    Description("Text to display in empty data row"),
    Category("Misc"),
    DefaultValue("),
    ]
    public string EmptyTableRowText
    {
        get
        {
            object o = ViewState["EmptyTableRowText"];
            return (o != null ? o.ToString() : ");
        }
        set
        {
            ViewState["EmptyTableRowText"] = value;
        }
    }

    #endregion


    protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
    {
        int numRows = base.CreateChildControls(dataSource, dataBinding);

        //no data rows created, create empty table if enabled
        if (numRows == 0 && ShowEmptyTable)
        {
            //create table
            Table table = new Table();
            table.ID = this.ID;

            //create a new header row
            GridViewRow row = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

            //convert the exisiting columns into an array and initialize
            DataControlField[] fields = new DataControlField[this.Columns.Count];
            this.Columns.CopyTo(fields, 0);
            this.InitializeRow(row, fields);
            table.Rows.Add(row);

            //create the empty row
            row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);
            TableCell cell = new TableCell();
            cell.ColumnSpan = this.Columns.Count;
            cell.Width = Unit.Percentage(100);
            cell.Controls.Add(new LiteralControl(EmptyTableRowText));
            row.Cells.Add(cell);
            table.Rows.Add(row);

            this.Controls.Add(table);
        }

        return numRows;
    }
}

You can download the code here. Reflector was helpful in seeing how grids were created in the framework as was Fredrik Normen's blog which always has great asp.net info.  Everytime i search for asp.net problem, its usually covered on his site.

Advertisement
Category: GridView | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 12710 | Hits: 205

Similar Posts

  • How to display data from different tables using one data source more
  • How to Make crossdomain.xml Work with SharePoint more
  • VS 2010 Code Intellisense Improvements (VS 2010 and .NET 4.0 Series) more
  • JavaScript class browser: once again with jQuery more
  • Telerik OpenAccess WCF Wizard October CTP more
  • Adhoc testing of .NET RIA Services more
  • Connecting to SQL Azure with Telerik OpenAccess more
  • What needed to be monitored to get better Governance more
  • Q3 2009 Release Week kicks-off November 4th, Free Daily Webinars more
  • A First Pass at PotterKata more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD