在Gridview中显示脚注行

在Gridview中显示脚注行,gridview,Gridview,我有一个包含GridView的VS2010 ASP.Net Web表单。此GridView具有ShowHeaderWhenEmpty=True,以便在网格的数据源为空时显示网格的标题 还有。GridView列是TemplateFields。第一列在HeaderTemplate中有一个新按钮。New button Click事件执行GridView1.FooterRow.Visible=True,以便用户可以在表中插入一行 问题是,当数据源为空且单击“新建”按钮时,会出现以下错误:对象引用未设置为

我有一个包含GridView的VS2010 ASP.Net Web表单。此GridView具有ShowHeaderWhenEmpty=True,以便在网格的数据源为空时显示网格的标题

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

问题是,当数据源为空且单击“新建”按钮时,会出现以下错误:对象引用未设置为对象的实例

如果数据源不是空的,并且单击了“新建”按钮,则一切正常

当数据源为空时,如何显示GridView的FooterRow

-更新-这是我提出的解决方案。
你可以这样做

一个更优雅的解决方案,在不篡改数据源的情况下,是创建一个扩展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;
    }
}

它显示页脚,但gridview.FooterRow将为空。有关解决方案,请参阅。
[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;
    }
}