1

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;
            }
            }
Jerry
  • 6,357
  • 8
  • 35
  • 50

1 Answers1

0

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;
    }
}
Yojimbo
  • 23,288
  • 5
  • 44
  • 48
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • It shows the footer, but gridview.FooterRow will be null. See http://stackoverflow.com/questions/994895/always-show-footertemplate-even-no-data/10891744#10891744 for the solution. – Aximili Jun 05 '12 at 04:55