Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a text in gridview "No Records Found" when there is no record

I have iframe where I have several charts and gridviews where the charts and gridviews are fetching data from database.when records found no issue but for some users don't have records for one of gridviews where I have to display a message "No records found".

kindly suggest me on this. I tried the below code which displays a message but it displays in small box,but I need to display inside the gridview.

   <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
   <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
       <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
       <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
 <EditRowStyle BackColor="#999999" />
 <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  <EmptyDataTemplate>No records Found</EmptyDataTemplate>

Here I am not showing grid header,but i have to!

like image 717
Chow.Net Avatar asked Jan 03 '13 02:01

Chow.Net


People also ask

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.

How to display Empty GridView in asp net c#?

The initial bind of an "empty" row will make the headers show and then the row being hidden will make it appear like an empty grid view. Show activity on this post. GridView1. DataSourceID = null; GridView1.

How can show empty GridView with header and footer in asp net?

The work around is to ensure that a dummy row is returned from the database when there is no real data present. You need to ensure that you have set the ShowFooter property of GridView to true. Another option to show header when GridView is empty is to set the GridView. ShowHeaderWhenEmpty property to true.

How do you sort data in GridView by clicking column header?

Start by adding a GridView to your web page. To make the columns sortable, you need to set the GridView's property AllowSorting = “true” and OnSorting = “OnSorting”. SortExpression property will hold the name of the column you want to sort.


2 Answers

Set ShowHeaderWhenEmpty property on the GridView to true.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.showheaderwhenempty.aspx

But you still have to DataBind the gridview. This code snippet worked for me:

    <asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True" EmptyDataText="No records Found">
    <Columns>
          <asp:boundfield datafield="CustomerID" headertext="Customer ID"/>
          <asp:boundfield datafield="Name" headertext="Name"/>
    </Columns>
   </asp:GridView>
like image 58
lahsrah Avatar answered Sep 17 '22 07:09

lahsrah


try setting emptydatatext and ShowHeaderWhenEmpty

<asp:gridview id="GridView" 
        datasourceid="DataSource" 
        autogeneratecolumns="true"
        emptydatatext="No data in the data source."
        runat="server"
        ShowHeaderWhenEmpty="True">
      </asp:gridview>
like image 43
COLD TOLD Avatar answered Sep 19 '22 07:09

COLD TOLD