Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6732237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:37:31+00:00 2026-05-26T10:37:31+00:00

I have a VS2010 ASP.Net webform that contains a GridView. This GridView has ShowHeaderWhenEmpty=True

  • 0

I have a VS2010 ASP.Net webform that contains a GridView. This GridView has ShowHeaderWhenEmpty=”True” so that the grid’s headers display when the Grid’s DataSource is empty.

Also. the GridView columns are TemplateFields. The first column has a new button in the HeaderTemplate. The New button Click event performs a GridView1.FooterRow.Visible = True so the user can insert a row into the table.

The problem is, when the DataSource is empty and the new button is clicked, I get this error: “Object reference not set to an instance of an object”.

If the DataSource is NOT empty and the new button is clicked, everything works fine.

How can I show the GridView’s FooterRow when the DataSource is empty?


–Update – This is the solution that I came up with.

           private BindMyGrid()
           {
            bool isHideFirstRow = false;
            if (_myEntity.Count > 0)
            {
                MyGrid.DataSource = _myEntity;
                MyGrid.DataBind();
            }
            else
            {
                //Create an empty list and bind to that.
                isHideFirstRow = true;
                var list = new List<MyEntity> { new MyEntity { col1 = "", col2 = "", Col2 = "", IsEnabled = false } };
                MyGrid.DataSource = list;
                MyGrid.DataBind();
            }

            if (isHideFirstRow)
            {
                //Hide the first row if it's empty
                MyGrid.Rows[0].Visible = false;
            }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:37:32+00:00Added an answer on May 26, 2026 at 10:37 am

    you can do like this….

    a more elegant solution, without tampering with the datasource, is creating a custom control that extends GridView and overriding CreateChildControls like so

    NOTE: This is an example on how to show footer and header also when the datasource of grid view is empty…….

    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 && ShowWhenEmpty)
        {
            //create table
            Table table = new Table();
            table.ID = this.ID;
    
            //convert the exisiting columns into an array and initialize
            DataControlField[] fields = new DataControlField[this.Columns.Count];
            this.Columns.CopyTo(fields, 0);
    
            if(this.ShowHeader)
            {
                //create a new header row
                GridViewRow headerRow = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
    
                this.InitializeRow(headerRow, fields);
                table.Rows.Add(headerRow);
            }
    
            //create the empty row
            GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
    
            TableCell cell = new TableCell();
            cell.ColumnSpan = this.Columns.Count;
            cell.Width = Unit.Percentage(100);
            if(!String.IsNullOrEmpty(EmptyDataText))
                cell.Controls.Add(new LiteralControl(EmptyDataText));
    
            if(this.EmptyDataTemplate != null)
                EmptyDataTemplate.InstantiateIn(cell);
    
            emptyRow.Cells.Add(cell);
            table.Rows.Add(emptyRow);
    
            if(this.ShowFooter)
            {
                //create footer row
                GridViewRow footerRow = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
    
                this.InitializeRow(footerRow, fields);
                table.Rows.Add(footerRow);
            }
    
            this.Controls.Clear();
            this.Controls.Add(table);
        }
        return numRows;
      }
    

    ShowWhenEmpty is a simple property that you set true to display headers and footers when empty:

    [Category("Behaviour")]
    [Themeable(true)]
    [Bindable(BindableSupport.No)]
    public bool ShowWhenEmpty
    {
        get     
        {
            if (ViewState["ShowWhenEmpty"] == null)
                ViewState["ShowWhenEmpty"] = false;
    
            return (bool)ViewState["ShowWhenEmpty"];
        }
        set
        {
            ViewState["ShowWhenEmpty"] = value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET web application I'm running with VS2010 that has session timeout
I have a ASP.NET MVC2 project in VS2010 that can be deployed in two
I have an Asp.net app that simply reads an xml file and this code
I have written an web application in VS2010 ASP.net C#, to display Crystal reports.
I have an ASP.net that I built in VS2010 running in iis7 however when
I have a page on my ASP.NET site that has a Repeater control to
I'm using MVC2 with VS2010 I have a view that has two partial views
I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
In VS2010 and ASP.NET MVC 2, it seems that client-side validation (JQuery Futures or
I have an ASP.NET MVC 2 application. Web project contains a reference to SomeProject

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.