Solved

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

Posted on 2013-01-29
5
492 Views
Last Modified: 2013-11-19
Hello

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
Sam
0
Comment
Question by:Samanthaoneill1502
5 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 38831556
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 38831795
Check this: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28009390.html

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.
0
 
LVL 10

Accepted Solution

by:
Monica P earned 500 total points
ID: 38834180
Checkout like this

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

Open in new window



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

Open in new window

0
 
LVL 10

Expert Comment

by:Monica P
ID: 38834184
0
 

Author Closing Comment

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

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.

778 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