GridView.EmptyDataTemplate Property (System.Web.UI.WebControls)

Switch View :
ScriptFree
.NET Framework Class Library
GridView.EmptyDataTemplate Property

Gets or sets the user-defined content for the empty data row rendered when a GridView control is bound to a data source that does not contain any records.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic
<BrowsableAttribute(False)> _
<PersistenceModeAttribute(PersistenceMode.InnerProperty)> _
<TemplateContainerAttribute(GetType(GridViewRow))> _
Public Overridable Property EmptyDataTemplate As ITemplate
C#
[BrowsableAttribute(false)]
[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
[TemplateContainerAttribute(typeof(GridViewRow))]
public virtual ITemplate EmptyDataTemplate { get; set; }
Visual C++
[BrowsableAttribute(false)]
[PersistenceModeAttribute(PersistenceMode::InnerProperty)]
[TemplateContainerAttribute(typeof(GridViewRow))]
public:
virtual property ITemplate^ EmptyDataTemplate {
	ITemplate^ get ();
	void set (ITemplate^ value);
}
F#
[<BrowsableAttribute(false)>]
[<PersistenceModeAttribute(PersistenceMode.InnerProperty)>]
[<TemplateContainerAttribute(typeof(GridViewRow))>]
abstract EmptyDataTemplate : ITemplate with get, set
[<BrowsableAttribute(false)>]
[<PersistenceModeAttribute(PersistenceMode.InnerProperty)>]
[<TemplateContainerAttribute(typeof(GridViewRow))>]
override EmptyDataTemplate : ITemplate with get, set
ASP.NET
<asp:GridView>
	<EmptyDataTemplate>ITemplate</EmptyDataTemplate>
</asp:GridView>

Property Value

Type: System.Web.UI.ITemplate
A System.Web.UI.ITemplate that contains the custom content for the empty data row. The default value is null, which indicates that this property is not set.
Remarks

The empty data row is displayed in a GridView control when the data source that is bound to the control does not contain any records. You can define your own custom user interface (UI) for the empty data row by using the EmptyDataTemplate property.

To specify a custom template for the empty data row, first place <EmptyDataTemplate> tags between the opening and closing tags of the GridView control. You can then list the contents of the template between the opening and closing <EmptyDataTemplate> tags. To control the style of the empty data row, use the EmptyDataRowStyle property. Alternatively, you can use the built-in UI for the empty data row by setting the EmptyDataText property instead of this property.

For information about how to programmatically access controls that you declare in an empty data template, see How to: Access Server Controls by ID.

Note Note

If both the EmptyDataText and EmptyDataTemplate properties are set, the EmptyDataTemplate property takes precedence.

Examples

The following example demonstrates how to define a custom template for the empty data row displayed when a GridView control is bound to a data source that does not contain any records.

Visual Basic


<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView EmptyDataTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView EmptyDataTemplate Example</h3>

      <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>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file. The following query        -->
      <!-- returns an empty data source to demonstrate the      -->
      <!-- empty row.                                           -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers] Where CustomerID='NoID'"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>

    </form>
  </body>
</html>



C#


<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView EmptyDataTemplate Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView EmptyDataTemplate Example</h3>

      <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>

      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file. The following query        -->
      <!-- returns an empty data source to demonstrate the      -->
      <!-- empty row.                                           -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers] Where CustomerID='NoID'"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>

    </form>
  </body>
</html>



Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also

Reference

Community Content

escatty
More than one year later with same problem
I cannot get the EmptyDataTemplate or EmptyDataText to work either.  And still no answer from anyone at Microsoft to the question above.  I've tried numerous ways in the code behind to place something in an empty grid to indicate that no data was returned and have yet to have it work.

asrfarinha
Can't get this to work
I can't get any of this to work as expected. I'm populating the GridView in the codebehind, and no matter what I set as the DataSource (tried null and an empty List), the EmptyDataTemplate or the EmptyDataText are never displayed.