Alex Rivera | Logout

How to check for an Empty Gridview

Asked 2009-04-08T19:28:28.383
17

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.

Edit
Report

1 Answer

19

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>
answered 2009-04-08T19:34:34.627

Your Answer