Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2013-01-29
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 20

Expert Comment

ID: 38831556
LVL 41

Expert Comment

ID: 38831795
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 2000 total points
ID: 38834180
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


Author Closing Comment

ID: 38838811
This worked perfectly for my situation.  Many thanks much appreciated.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

704 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