在Gridview中显示FooterRow

时间:2011-10-25 15:44:47

标签: gridview

我有一个包含GridView的VS2010 ASP.Net webform。此GridView具有ShowHeaderWhenEmpty =“True”,以便在Grid的DataSource为空时显示网格的标题。

另外。 GridView列是TemplateFields。第一列在HeaderTemplate中有一个新按钮。 New按钮Click事件执行GridView1.FooterRow.Visible = True,因此用户可以在表中插入一行。

问题是,当DataSource为空并且单击了新按钮时,我收到此错误:“对象引用未设置为对象的实例”。

如果DataSource不为空并且单击了新按钮,则一切正常。

当DataSource为空时,如何显示GridView的FooterRow?


- 更新 - 这是我提出的解决方案。

           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 个答案:

答案 0 :(得分:0)

你可以这样做....

一个更优雅的解决方案,没有篡改数据源,正在创建一个自定义控件,扩展GridView并覆盖CreateChildControls,如此

注意:这是一个关于如何在网格视图的数据源为空时显示页脚和页眉的示例.......

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是一个简单属性,您将其设置为true以在空白时显示页眉和页脚:

[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;
    }
}