网格视图 asp.net 如果没有数据则显示标题

发布于 2024-10-04 11:56:45 字数 58 浏览 0 评论 0原文

即使绑定到网格的数据源为空,我也想显示网格视图的标题?有没有办法在不添加空白行的情况下实现相同的目的?

i want to show a grid view' header even if the data source that bound to the grid is empty? Is there any way to achieve the same without adding a BLANK row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揪着可爱 2024-10-11 11:56:45

从 ASP.NET 4 开始,您可以将 GridView 的 ShowHeaderWhenEmpty 属性设置为 true

As of ASP.NET 4, you can set the ShowHeaderWhenEmpty property of the GridView to true.

两相知 2024-10-11 11:56:45

另一种选择是将 GridView 的 ShowHeaderWhenEmpty 属性设置为 true。请注意,这适用于 .Net Framework 4.0 及更高版本。
learn.microsoft.com

Another option is to set the ShowHeaderWhenEmpty property of GridView to true. Note that this is be applicable to .Net Framework 4.0 and higher.
learn.microsoft.com

メ斷腸人バ 2024-10-11 11:56:45

最简单的方法是创建您自己的继承 GridView 类的 GridView。然后重写 CreateChildControls 方法来创建一个新的空表。

像这样的事情应该有效:

protected GridViewRow _footerRow2;
protected GridViewRow _headerRow2;

protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
    // Call base method and get number of rows
    int numRows = base.CreateChildControls(dataSource, dataBinding);

    if (numRows == 0)
    {
        //no data rows created, create empty table
        //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
            _headerRow2 = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

            this.InitializeRow(_headerRow2, fields);
            _headerRow2.EnableTheming = true;
            table.Rows.Add(_headerRow2);
        }

        if (this.ShowFooter)
        {
            //create footer row
            _footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);

            this.InitializeRow(_footerRow2, fields);
            _footerRow2.EnableTheming = true;
            table.Rows.Add(_footerRow2);
        }

        this.Controls.Clear();
        this.Controls.Add(table);
    }

    return numRows;
}

基本上,您检查 GridView 是否有任何行,如果没有,则创建标题行和页脚行(如果它们已启用)。

编辑:

此外,如果您仍想显示 EmptyDataText,您可以在创建页眉和页脚之间添加这些行。

GridViewRow emptyRow;

if (this.EmptyDataTemplate != null)
{
     emptyRow = this.Controls[0].Controls[0] as GridViewRow;
}
table.Rows.Add(emptyRow);

The easiest way would be to create you own GridView inheriting from the GridView class. Then override the CreateChildControls method to create a new empty table.

Something like this should work:

protected GridViewRow _footerRow2;
protected GridViewRow _headerRow2;

protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
    // Call base method and get number of rows
    int numRows = base.CreateChildControls(dataSource, dataBinding);

    if (numRows == 0)
    {
        //no data rows created, create empty table
        //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
            _headerRow2 = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

            this.InitializeRow(_headerRow2, fields);
            _headerRow2.EnableTheming = true;
            table.Rows.Add(_headerRow2);
        }

        if (this.ShowFooter)
        {
            //create footer row
            _footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);

            this.InitializeRow(_footerRow2, fields);
            _footerRow2.EnableTheming = true;
            table.Rows.Add(_footerRow2);
        }

        this.Controls.Clear();
        this.Controls.Add(table);
    }

    return numRows;
}

Basically, you check if the GridView has any rows and if it doesn't then you create the header row and footer row (if they are enabled).

EDIT:

Also, if you wanted to still show your EmptyDataText, you could add these lines inbetween the creating of the header and footer.

GridViewRow emptyRow;

if (this.EmptyDataTemplate != null)
{
     emptyRow = this.Controls[0].Controls[0] as GridViewRow;
}
table.Rows.Add(emptyRow);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文