How to check for an Empty Gridview How to check for an Empty Gridview asp.net asp.net

How to check for an Empty Gridview


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


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>


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 zeroGridView.DataBind();rowCount = GridView.Rows.Count; // returns actual row count