GridView not showing contents of EmptyDataTemplate and grid headers at same time

Posted on 2013-01-29
Last Modified: 2013-11-19

I have a gridview with multiple columns ecah defined within an <asp:TemplateField> tag.  Now when I have no rows returned from my DB  I want my grid to retain its headers but also show the "No Rows Returned" message.

Normally I do this by binding a single empty object within an array as my returned results as follows:

if (list == null || list.Length == 0)
      list = new MyGridListItem[] { new MyGridListItem() };

 return list;

But for some reason when I do this I seeing my grid headers and then an empty row in the grid instead of the "No Rows Returned" message??  If I simply return null from this method then I get my message but no grid headers.

Has anyone any idea whay this should be?

Many thanks
Question by:Samanthaoneill1502
LVL 20

Expert Comment

Comment Utility
LVL 41

Expert Comment

Comment Utility
Check this:

Why it's doing that is because EmptyDataItemTemplate is shown if the datasource is null. Since you array with one item, it's not considered empty.
LVL 10

Accepted Solution

Monica P earned 500 total points
Comment Utility
Checkout like this

<asp:GridView ID="gvdata" runat="server" CssClass="Gridview" AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White">
<asp:BoundField DataField="UserName" HeaderText="Name"/>
<asp:BoundField DataField ="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" />

Open in new window

protected void BindGridviewData()
SqlConnection con = new SqlConnection("Data Source=xxx;Initial Catalog=testDB;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select UserName,FirstName,LastName,Location from UserDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
gvdata.DataSource = ds;
int columncount = gvdata.Rows[0].Cells.Count;
gvdata.Rows[0].Cells.Add(new TableCell());
gvdata.Rows[0].Cells[0].ColumnSpan = columncount;
gvdata.Rows[0].Cells[0].Text = "No Records Found";
gvdata.DataSource = ds;

Open in new window

LVL 10

Expert Comment

by:Monica P
Comment Utility

Author Closing Comment

Comment Utility
This worked perfectly for my situation.  Many thanks much appreciated.

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now