In this article I will explain a tutorial with an example, how to use the EmptyDataTemplate property of ASP.Net GridView in C# and VB.Net.
The EmptyDataTemplate property of ASP.Net GridView is used to display Empty (No records found) message when GridView is empty i.e. does not have data.
 
 
Database
I have made use of the following table Customers with the schema as follows.
EmptyDataTemplate in GridView Tutorial with example in ASP.Net
 
I have already inserted few records in the table.
EmptyDataTemplate in GridView Tutorial with example in ASP.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView and an ASP.Net RadioButtonList.
RadioButtonList
The RadioButtonList has been specified with OnSelectedIndexChanged event handler and the AutoPostBack property is set to True.
 
GridView
The ShowHeaderWhenEmpty property of the GridView control is set to True so that even when there is no data, the GridView will display the Header row.
The EmptyDataTemplate property has been specified with an Empty Message i.e. No records found which will be displayed when the GridView is empty.
<asp:RadioButtonList ID="rblCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Country_Selected" RepeatDirection = "Horizontal">
    <asp:ListItem Text="All" Value="" Selected = "True"></asp:ListItem>
    <asp:ListItem Text="India" Value="India"></asp:ListItem>
    <asp:ListItem Text="United States" Value="United States"></asp:ListItem>
    <asp:ListItem Text="France" Value="France"></asp:ListItem>
    <asp:ListItem Text="Russia" Value="Russia"></asp:ListItem>
    <asp:ListItem Text="Canada" Value="Canada"></asp:ListItem>
</asp:RadioButtonList>
<hr />
<asp:GridView ID="GridView1" runat="server" CssClass = "Grid" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
    <EmptyDataTemplate>
        <div align="center">No records found.</div>
    </EmptyDataTemplate>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
 
 
Populating the GridView control
Inside the Page Load event, the BindGrid function is called.
Inside the BindGrid function, first the Selected Value of the RadioButtonList is fetched and it is passed to the SQL Query using Parameters.
The SQL Query is written in such a way that if the Selected Value of the RadioButtonList is Blank (Empty) then all records will be displayed else the filtered records will be displayed.
Once the SQL Query is executed the GridView is populated using the records from the Customers table.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}
 
private void BindGrid()
{
    string country = rblCountries.SelectedItem.Value;
    string sql = "SELECT * FROM Customers WHERE Country=@Country OR @Country = ''";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(sql))
        {
            cmd.Parameters.AddWithValue("@Country", country);
            cmd.Connection = con;
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGrid()
    End If
End Sub
 
Private Sub BindGrid()
    Dim country As String = rblCountries.SelectedItem.Value
    Dim sql As String = "SELECT * FROM Customers WHERE Country=@Country OR @Country = ''"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(sql)
            cmd.Parameters.AddWithValue("@Country", country)
            cmd.Connection = con
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Using dt As DataTable = New DataTable()
                    sda.Fill(dt)
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                End Using
            End Using
        End Using
    End Using
End Sub
 
 
The RadioButtonList SelectedIndexChanged event handler
The following event handler gets triggered when a selection is made in the RadioButtonList. Inside this event handler, the BindGrid function is called.
C#
protected void Country_Selected(object sender, EventArgs e)
{
    this.BindGrid();
}
 
VB.Net
Protected Sub Country_Selected(ByVal sender As Object, ByVal e As EventArgs)
    Me.BindGrid()
End Sub
 
 
Screenshot
EmptyDataTemplate in GridView Tutorial with example in ASP.Net
 
 
Demo
 
 
Downloads