Displaying empty grid message in asp.net using C#

In this tutorial we will learn how to display empty grid message when grid contains no record. It is better to display a suitable message such as No Record Found when grid is empty.

In asp.net using C# it is quite easy to display empty grid message.

There is couple of ways to display empty grid message in C#. Let’s have a look over them.

Method 1)

Use EmptyDataText attribute in gridview tag if you want to show empty grid message

Let’s look over its implementation.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%" HeaderStyle-HorizontalAlign="Left" HeaderStyle-VerticalAlign="Top" EmptyDataText="No Record Found" EmptyDataRowStyle-ForeColor="Maroon" EmptyDataRowStyle-HorizontalAlign="Center" GridLines="None">

Now you will get No Record Found text in Maroon Color at center align when there is no data in the grid.

Method 2)

Use <EmptyDataTemplate> after </columns> inside the <asp:GridView></asp:Gridview>

Let’s look over its implementation.

<asp:gridview id="GridView1" runat="server" width="100%">
<Columns>
<asp:TemplateField HeaderText="Email" >
<ItemTemplate>
<asp:Label ID="lbl_Email" Text='<%# Bind("User_Email")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" >
<ItemTemplate>
<asp:Label ID="lbl_first_name" Text='<%# Bind("User_First_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" >
<ItemTemplate>
<asp:Label ID="lbl_last_name" CssClass="aLinkButton" Text='<%# Bind("User_Last_Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="15px" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="lbl_RecordNotFound" Text="No Record Found" runat="server" Font-Size="Larger" ForeColor="maroon" ></asp:Label>
</EmptyDataTemplate>

</asp:gridview>

Now if you want to show empty grid message underneath the grid header then add table inside the <EmptyDataTemplate> according to the your gridview header, the most feasible way to show empty grid message with grid header is to right click your aspx page in the web browser (when grid contains some data), go to view source and then copy grid header code, normally grid header lie inside a table, the benefit of doing such is that if you have applied any CSS Class to gridview then that class will also be implemented automatically on the Empty Grid Message.

Let’s look over its implementation.

<EmptyDataTemplate>
<table cellpadding="2" cellspacing="2" width="100%">
<tr>
<td> Email </td><td> First Name </td><td>Last Name</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Label ID="lbl_RecordNotFound" Text="No Record Found" runat="server" Font-Size="Larger" ForeColor="maroon" ></asp:Label>
</td>
</tr>
</table>
</EmptyDataTemplate>

So these are the methods to show empty grid message when grid is empty or grid contains no record.

0 comments: