GridView ASP if the table is empty

How can I display a message instead of a GridView if there are no results from the database?

+4
source share
5 answers

GridView has a template and style EmptyDataRow , just use this:

  <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 with .NET 2.0)

+7
source

There is also an EmptyDataText property that you enter on <asp:GridView ... >

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

and EmptyDataRowStyle-CssClass to style it.

+4
source

You can use the EmptyDataText property.

 GridView1.EmptyDataText = "No data found"; 

Another way, but the one above is better suited.

 if (dt != null && dt.Rows.Count == 0) { lblAfterGridGridView1.TextEmptyDataText = "No recorddata found"; } if(dt.Rows.Count > 0) { //Show grid here lblAfterGrid.Text = ""; } 
+1
source

You can also put directly in your code: C #

 GridView1.EmptyDataText = "This table has no data, or whatever"; 

This approach is good because you can control when to display this message.

those. in my case, when the first page appears (! IsPostback), I can say: "Please select the fields ..."

and if the request is empty, I can say: "Sorry, no data found ...."

+1
source

use the EmptyDataText property to set the text of the empty grid. and just bind your DataGrid with a null value to open the empty text.

 if(dt.Rows.Count > 0) { //call Bind grid method here } else { Grid.DataSource=null; Grid.DataBind(); } 
0
source

Source: https://habr.com/ru/post/1438132/


All Articles