Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

EmptyDataText and EmptyDataTemplate properties of asp.net gridview control - Part 27

Suggested Videos 
Part 24 - GridView insert update delete in asp.net using objectdatasource
Part 25 - GridView insert update delete without using datasource controls
Part 26 - Displaying summary data in asp.net gridview footer



By default an asp.net gridview will not display anything on the webform, if there are no rows in the datasource to which the gridview is bound. For example, let us say we are binding a gridview control to tblProduct table. If there are no rows in this table, then the gridview does not render any thing on the webform. If there are no rows, I want the gridview to display a message stating - "There are no products to display".



1. Set EmptyDataText property of the gridview control as shown below
<asp:GridView ID="GridView1" EmptyDataText="There are no products to display" ...
</asp:GridView>

OR

2. Use EmptyDataTemplate as shown below
<EmptyDataTemplate>
    There are no products to display
</EmptyDataTemplate>

EmptyDataRowStyle Property can be used to set the display styles of the empty data row.

If you set both, EmptyDataTemplate is used instead of EmptyDataText. The difference, between the 2 is that, if you want to have greater on what you want to display, then use EmptyDataTemplate.

For example, I want the gridview to render an HTML table with a header and a message as shown below.
EmptyDataTemplate

To achieve this I would use the following EmptyDataTemplate
<EmptyDataTemplate>
    <table cellspacing="2" cellpadding="3" rules="all" id="GridView1" style="background-color: #DEBA84;
        border-color: #DEBA84; border-width: 1px; border-style: None;">
        <tr style="color: White; background-color: #A55129; font-weight: bold;">
            <td scope="col">
                ProdcutId
            </td>
            <td scope="col">
                Name
            </td>
            <td scope="col">
                UnitPrice
            </td>
            <td scope="col">
                QuantitySold
            </td>
        </tr>
        <tr style="color: #8C4510; background-color: #FFF7E7;">
            <td colspan="4">
                There are no products to display
            </td>
        </tr>
    </table>
</EmptyDataTemplate>

No comments:

Post a Comment

It would be great if you can help share these free resources