C# 如何检查空的 Gridview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/731461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:59:13  来源:igfitidea点击:

How to check for an Empty Gridview

c#asp.netgridviewnull

提问by zohair

I have an ASP.NET 2.0 (C#) web app, and in it I have a gridview that gets its data from an oracle database.

我有一个 ASP.NET 2.0 (C#) web 应用程序,在其中我有一个 gridview,可以从 oracle 数据库中获取数据。

I want to know how to check if the gridview is empty, and the do something.

我想知道如何检查 gridview 是否为空,然后做一些事情。

I have already tried:

我已经尝试过:

if(GridView.Rows.Count == 0)
{
// Do Something
}

but it doesn't work...

但它不起作用......

Any ideas?

有任何想法吗?

Thank you.

谢谢你。

采纳答案by Al W

Your code shouldwork. But only after GridView.DataBind() has been called. Generally I don't check the GridView it's self, but the datasource of the grid view.

您的代码应该可以工作。但只有在调用 GridView.DataBind() 之后。通常我不检查它自己的 GridView,而是检查网格视图的数据源。

DataTable data = DAL.getdata();
if (data.Rows.Count == 0)
{
    ShowEmptyData();
}
else
{
    Grid.DataSource = dt;
    Grid.DataBind();
}

回答by Justin Niessner

If you're using databinding, the the row count of the datasource not the count on the grid itself.

如果您使用数据绑定,则数据源的行数不是网格本身的计数。

回答by Mehrdad Afshari

This doesn't work since the GridViewis data bound and is going to fetch the actual data at a later time while rendering the page. You should check this by directly querying the data binding source of the gridview (see if the actual list that's grid view bound to is empty or not).

这不起作用,因为它GridView是数据绑定的,并且将在稍后呈现页面时获取实际数据。您应该通过直接查询 gridview 的数据绑定源来检查这一点(查看网格视图绑定到的实际列表是否为空)。

If you just want to display something when it's empty, you should use <EmptyDataTemplate>in your markup:

如果你只想在空的时候显示一些东西,你应该<EmptyDataTemplate>在你的标记中使用:

<asp:GridView runat="server">
<EmptyDataTemplate>The grid is empty</EmptyDataTemplate>
</asp:GridView>

回答by Canavar

I agree with the other responses. I want to add little information, you should get rows.count after databind method :

我同意其他回答。我想添加一些信息,你应该在 databind 方法之后得到 rows.count :

int rowCount = GridView.Rows.Count; // returns zero

GridView.DataBind();

rowCount = GridView.Rows.Count; // returns actual row count

回答by Aki

Its very easy: Hope it works for you.. :)

它非常简单:希望它对你有用.. :)

Use event of GridView DataBound: which fires after data is bound.

使用 GridView DataBound 事件:数据绑定后触发。

 protected void GridView1_DataBound(object sender, EventArgs e)
 {
     int rowCount = GridView1.Rows.Count;

     if (rowCount == 0)
     {
         GridView1.Visible = false;                
     }
     else
     {
        GridView1.Visible = true;
     }

 }

回答by olleh

In case you've configured your GV to automatically fetch the data from DB, then you may add the following line as the first statement of your GV in Source mode:

如果您已将 GV 配置为自动从数据库获取数据,那么您可以添加以下行作为源模式下 GV 的第一条语句:

<EmptyDataTemplate>No data found.</EmptyDataTemplate>

And then continue with the normal code in your GV.

然后继续使用 GV 中的正常代码。

回答by nerdybeardo

First create a helper class.

首先创建一个辅助类。

public static class GridViewExtensions
{
    public static IEnumerable<GridViewRow> AsEnumerable(this GridView grid)
    {
        foreach (GridViewRow row in grid.Rows)
        {
            yield return row;
        }
    }

    public static bool IsEmpty(this GridView grid)
    {
        return !grid.AsEnumerable().Any(t => t.RowType == DataControlRowType.DataRow);
    }
}

Then just call IsEmpty

然后只需调用 IsEmpty

GridView1.IsEmpty()

回答by Fandango68

Based on answers already, GridView.Rows.Countis not enough on its own, depending on the nature of your GridView, especially if it's a dynamic gv, which in most cases it is, plus you have to factor in Paginating, Headers and Footers, which alter the row count.

已经基于答案GridView.Rows.Count,这本身是不够的,这取决于您的 性质GridView,特别是如果它是动态 gv,在大多数情况下是这样,而且您必须考虑Paginating,页眉和页脚,它们会改变行数。

I use a simple method to tell me ...

我用一个简单的方法告诉我...

//checks if a gridview has any actual rows of data (not just blank rows filled in by the Load
protected bool gvNoData(GridView gv)
{
    int wsDataRow = 0;
    foreach (GridViewRow gvRow in gv.Rows)
        if (gvRow.RowType == DataControlRowType.DataRow)
        {
            HiddenField hf = (HiddenField)gvRow.FindControl("hfStudentID");
            if (hf != null)
                if (hf.Value.ToString().Length > 0)
                    wsDataRow +=1;
        }

    //if a count was generated then there are data rows, otherwise the rows are blank or nonexistant
    if (wsDataRow > 0) return false;
    else return true;
}

So running something like this will tell you if the Rows are really " "DATA" rows, or empty or nothing!

所以运行这样的东西会告诉你行是否真的是“数据”行,还是空行或什么都没有!

Obviously in my case I have a HiddenField to tell me if the GridViewRow is an actual data row, as I prefill my gridview with blank rows (for my purposes) and some datarows.

显然,在我的情况下,我有一个 HiddenField 来告诉我 GridViewRow 是否是实际数据行,因为我用空白行(出于我的目的)和一些数据行预填充了我的网格视图。

However, a simpler version to check based on DataRow vs HeaderRow, etc...

但是,基于 DataRow 与 HeaderRow 等进行检查的更简单版本...

        foreach (GridViewRow gvRow in myGridView.Rows)
            if (gvRow.RowType == DataControlRowType.DataRow)
            {
//do what you need
            }

I hope this helps.

我希望这有帮助。

In short, there is no GridView.IsEmpty() function unfortunately, unless you enumerate one as shown below.

简而言之,不幸的是,没有 GridView.IsEmpty() 函数,除非您枚举如下所示的一个。