Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView ASP if the table is empty

Tags:

c#

asp.net

How can I display a message in the place of the GridView if there is no results come from the database?

like image 267
Fady Avatar asked Dec 12 '22 21:12

Fady


2 Answers

The GridView has an EmptyDataRow template and style, just make the use of that:

  <asp:gridview id="CustomersGridView" 
    datasourceid="CustomersSqlDataSource" 
    autogeneratecolumns="true"
    runat="server">

    <emptydatarowstyle backcolor="LightBlue"
      forecolor="Red"/>

    <emptydatatemplate>

      <asp:image id="NoDataImage"
        imageurl="~/images/Image.jpg"
        alternatetext="No Image" 
        runat="server"/>

        No Data Found.  

    </emptydatatemplate> 

  </asp:gridview>

example from MSDN GridView.EmptyDataRowStyle Property (available since .NET 2.0)

like image 144
balexandre Avatar answered Jan 01 '23 13:01

balexandre


There is also the EmptyDataText property that you type on the <asp:GridView ... >

EmptyDataText="There is no items in the list box"

and the EmptyDataRowStyle-CssClass to style it.

like image 43
Aristos Avatar answered Jan 01 '23 12:01

Aristos