zohair zohair -4 years ago 123
ASP.NET (C#) Question

How to check for an Empty Gridview

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.

Answer Source

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();
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download