如何检查 Gridview 是否为空

发布于 2024-07-16 09:43:32 字数 256 浏览 3 评论 0原文

我有一个 ASP.NET 2.0 (C#) Web 应用程序,其中有一个从 Oracle 数据库获取数据的 gridview。

我想知道如何检查 gridview 是否为空,并执行某些操作。

我已经尝试过:

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

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

有什么想法吗?

谢谢。

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.

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

I have already tried:

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

but it doesn't work...

Any ideas?

Thank you.

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

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

发布评论

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

评论(8

木緿 2024-07-23 09:43:32

您的代码应该可以工作。 但只有在调用 GridView.DataBind() 之后。 一般来说,我不检查 GridView 本身,而是检查网格视图的数据源。

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

Your code should work. 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.

DataTable data = DAL.getdata();
if (data.Rows.Count == 0)
{
    ShowEmptyData();
}
else
{
    Grid.DataSource = dt;
    Grid.DataBind();
}
旧时模样 2024-07-23 09:43:32

这是行不通的,因为 GridView 是数据绑定的,并且稍后会在渲染页面时获取实际数据。 您应该通过直接查询网格视图的数据绑定源来检查这一点(查看网格视图绑定的实际列表是否为空)。

如果您只想在空时显示某些内容,则应在标记中使用

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

This doesn't work since the GridView is 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).

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

<asp:GridView runat="server">
<EmptyDataTemplate>The grid is empty</EmptyDataTemplate>
</asp:GridView>
壹場煙雨 2024-07-23 09:43:32

我同意其他回复。 我想添加一点信息,您应该在 databind 方法之后获得 rows.count :

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

GridView.DataBind();

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

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

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

GridView.DataBind();

rowCount = GridView.Rows.Count; // returns actual row count
玩物 2024-07-23 09:43:32

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

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

别想她 2024-07-23 09:43:32

它非常简单:希望它适合您..:)

使用 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;
     }

 }

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

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

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

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

 }
墨落成白 2024-07-23 09:43:32

首先创建一个辅助类。

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

然后只需调用 IsEmpty

GridView1.IsEmpty()

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

GridView1.IsEmpty()
掀纱窥君容 2024-07-23 09:43:32

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

<EmptyDataTemplate>No data found.</EmptyDataTemplate>

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

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:

<EmptyDataTemplate>No data found.</EmptyDataTemplate>

And then continue with the normal code in your GV.

蔚蓝源自深海 2024-07-23 09:43:32

根据已经的答案, GridView.Rows.Count 本身是不够的,具体取决于 GridView 的性质,特别是如果它是动态 gv,在大多数情况下确实如此,而且您还必须考虑分页、页眉和页脚,它们会改变行数。

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

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

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

显然,在我的例子中,我有一个 HiddenField 来告诉我 GridViewRow 是否是实际的数据行,因为我用空白行(出于我的目的)和一些数据行预填充了我的 gridview。

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

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

我希望这会有所帮助。

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

Based on answers already, GridView.Rows.Count is 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.

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.

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

        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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文