Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 620
zohair Avatar asked Apr 08 '09 19:04

zohair


People also ask

How to show no record found in GridView?

HTML MarkupThe ShowHeaderWhenEmpty property of the GridView control is set to True so that even when there is no data, the GridView will display the Header row. The EmptyDataTemplate property has been specified with an Empty Message i.e. No records found which will be displayed when the GridView is empty.

How do you display the empty GridView in case of no records in database?

To do that, we need to enable the Boolean property ShowHeaderWhenEmpty to True. Be sure you're using the ASP.NET 4.0 or later version to use this property. Also, we need to add the <EmptyDataTemplate></EmptyDataTemplate> property inside the grid view to display the message.


1 Answers

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();
}
like image 114
Al W Avatar answered Sep 19 '22 13:09

Al W