GridView - Show headers on empty data source

asp.netGridviewHeader

asp.net Problem Overview


In C# how do I still show the headers of a gridview, even with the data source is empty.

I am not auto generating the columns as they are all predefined.

Currently what I am doing is the following.

Get a DataTable back from a stored procedure, then set the DataSource of the gridview, and then call DataBind().

This works fine when I have data, but when no rows are returned then I just get a blank spot where the grid should be.

Edit: Thanks all for the .NET 4+ property. I asked this back in the .NET 3.5 days. This is much easier now. :)

asp.net Solutions


Solution 1 - asp.net

ASP.Net 4.0 added the boolean ShowHeaderWhenEmpty property.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.showheaderwhenempty.aspx


<asp:GridView runat="server" ID="GridView1" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="First Name" DataField="FirstName" />
        <asp:BoundField HeaderText="Last Name" DataField="LastName" />
    </Columns>
</asp:GridView>

Note: the headers will not appear unless DataBind() is called with something other than null.

GridView1.DataSource = New List(Of String)
GridView1.DataBind()

Solution 2 - asp.net

After posting this I did come up with a way that works. However, I don't feel it is the best way to handle this. Any suggestions on a better one?

//Check to see if we get rows back, if we do just bind.

if (dtFunding.Rows.Count != 0)
{
    grdFunding.DataSource = dtFunding;
    grdFunding.DataBind();
}
else
{
  //Other wise add a emtpy "New Row" to the datatable and then hide it after binding.

     dtFunding.Rows.Add(dtFunding.NewRow());
     grdFunding.DataSource = dtFunding;
     grdFunding.DataBind();
     grdFunding.Rows[0].Visible = false;
}

Solution 3 - asp.net

I was just working through this problem, and none of these solutions would work for me. I couldn't use the EmptyDataTemplate property because I was creating my GridView dynamically with custom fields which provide filters in the headers. I couldn't use the example almny posted because I'm using ObjectDataSources instead of DataSet or DataTable. However, I found this answer posted on another StackOverflow question, which links to this elegant solution that I was able to make work for my particular situation. It involves overriding the CreateChildControls method of the GridView to create the same header row that would have been created had there been real data. I thought it worth posting here, where it's likely to be found by other people in a similar fix.

Solution 4 - asp.net

If you are working with ASP.NET 3.5 and lower, and your problem is relatively simple like mine, you can just return a null row from the SQL query.

if not exists (select RepId, startdate,enddate from RepTable where RepID= 10)
     select null RepID,null StartDate,null EndDate
else
     select RepId, startdate,enddate from RepTable where RepID= 10

This solution does not require any C# code or ASP.NET code

  1. Make sure you cast the null columns into appropriate names, otherwise it will not work.
  2. Else block must be included which is the same query as in if not exists (query part)
  3. In my case if I am using @RepID instead of 10. Which is mapped to a DropDownList box outside gridview.

Each time I change the drop down to select a different rep, Gridview is updated. If no record is found, it shows a null row.

Solution 5 - asp.net

set "<asp:GridView AutoGenerateColumns="false" ShowHeaderWhenEmpty="true""

showheaderwhenEmpty Property

Solution 6 - asp.net

You can use HeaderTemplate property to setup the head programatically or use ListView instead if you are using .NET 3.5.

Personally, I prefer ListView over GridView and DetailsView if possible, it gives you more control over your html.

Solution 7 - asp.net

You can set the ShowHeadersWhenNoRecords property of the ownertableview to true. aspx:

<asp:GridView ID="RadGrid2" runat="server" >       
<MasterTableView ShowHeadersWhenNoRecords="true"  > 

Also when the datasource for the GridView is null(when no records), you can try setting it as shown below: c#:

  if (GridView1.DataSource == null)  
  {  
        GridView1.DataSource = new string[] { };  
  } 
  GridView1.DataBind();

Solution 8 - asp.net

Add this property to your grid-view : ShowHeaderWhenEmpty="True" it might help just check

Solution 9 - asp.net

I found a very simple solution to the problem. I simply created two GridViews. The first GridView called a DataSource with a query that was designed to return no rows. It simply contained the following:

    <Columns>
        <asp:TemplateField HeaderStyle-HorizontalAlign="Left">
            <HeaderTemplate>

               <asp:Label ID="lbl0" etc.>  </asp:Label>
               <asp:Label ID="lbl1" etc.>  </asp:Label>

            </HeaderTemplate>
        </asp:TemplateField>
    </Columns>

Then I created a div with the following characteristics and I place a GridView inside of it with ShowHeader="false" so that the top row is the same size as all the other rows.

<div style="overflow: auto; height: 29.5em; width: 100%">
    <asp:GridView ID="Rollup" runat="server" ShowHeader="false" DataSourceID="ObjectDataSource">
        <Columns>
            <asp:TemplateField HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>

               <asp:Label ID="lbl0" etc.>  </asp:Label>
               <asp:Label ID="lbl1" etc.>  </asp:Label>

                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>

Solution 10 - asp.net

<asp:GridView ID="grdGroup"  EmptyDataText="No Records Found" ShowHeaderWhenEmpty="True" runat="server">

This is a basic example of Gridview with EmptyDataText and ShowHeaderWhenEmpty

Solution 11 - asp.net

Juste add ShowHeaderWhenEmpty property and set it at true

This solution works for me

Solution 12 - asp.net

I was using asp sqlDataSource. It worked for me when I set the CancelSelectOnNullParameter to false as below:

<asp:SqlDataSource ID="SqlData1" runat="server" ConnectionString="" SelectCommand="myStoredProcedure" SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False"> </asp:SqlDataSource>

Solution 13 - asp.net

<asp:GridView ID="gvEmployee" runat="server"    
                 AutoGenerateColumns="False" ShowHeaderWhenEmpty=”True”>  
                    <Columns>  
                        <asp:BoundField DataField="Id" HeaderText="Id" />  
                        <asp:BoundField DataField="Name" HeaderText="Name" />  
                        <asp:BoundField DataField="Designation" HeaderText="Designation" />  
                        <asp:BoundField DataField="Salary" HeaderText="Salary"  />  
                    </Columns>  
                    <EmptyDataTemplate>No Record Available</EmptyDataTemplate>  
                </asp:GridView>  


in CS Page

gvEmployee.DataSource = dt;  
gvEmployee.DataBind();  

Solution 14 - asp.net

You may use EmptyDataText as shown below:

<asp:GridView ID="_gridView" RunAt="server" AutoGenerateColumns="false"
          EmptyDataText="No entries found.">

It does not show headers, it renders your message "No entries found." instead.

Solution 15 - asp.net

    <asp:GridView ID="gvEmployee" runat="server"    
                     AutoGenerateColumns="False" ShowHeaderWhenEmpty=”True”>  
                        <Columns>  
                            <asp:BoundField DataField="Id" HeaderText="Id" />  
                            <asp:BoundField DataField="Name" HeaderText="Name" />  
                            <asp:BoundField DataField="Designation" HeaderText="Designation" />  
                            <asp:BoundField DataField="Salary" HeaderText="Salary"  />  
                        </Columns>  
                        <EmptyDataTemplate>No Record Available</EmptyDataTemplate>  
                    </asp:GridView>  
    
    
    in CS Page
    
    gvEmployee.DataSource = dt;  
    gvEmployee.DataBind();  

Help.. see that link:
http://www.c-sharpcorner.com/UploadFile/d0e913/how-to-display-the-empty-gridview-in-case-of-no-records-in-d/

Solution 16 - asp.net

Use an EmptyDataTemplate like below. When your DataSource has no records, you will see your grid with headers, and the literal text or HTML that is inside the EmptyDataTemplate tags.

<asp:GridView ID="gvResults" AutoGenerateColumns="False" HeaderStyle-CssClass="tableheader" runat="server">
	<EmptyDataTemplate>
		<asp:Label ID="lblEmptySearch" runat="server">No Results Found</asp:Label>
	</EmptyDataTemplate>
	<Columns>
		<asp:BoundField DataField="ItemId" HeaderText="ID" />
		<asp:BoundField DataField="Description" HeaderText="Description" />
		...
	</Columns>
</asp:GridView>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoshua HudsonView Question on Stackoverflow
Solution 1 - asp.netzacharydlView Answer on Stackoverflow
Solution 2 - asp.netJoshua HudsonView Answer on Stackoverflow
Solution 3 - asp.netStriplingWarriorView Answer on Stackoverflow
Solution 4 - asp.netHammad KhanView Answer on Stackoverflow
Solution 5 - asp.netvikram JangraView Answer on Stackoverflow
Solution 6 - asp.netLiwenView Answer on Stackoverflow
Solution 7 - asp.netkezView Answer on Stackoverflow
Solution 8 - asp.nettariqView Answer on Stackoverflow
Solution 9 - asp.netaneginView Answer on Stackoverflow
Solution 10 - asp.netuser2753577View Answer on Stackoverflow
Solution 11 - asp.netonlymeView Answer on Stackoverflow
Solution 12 - asp.netMd Toufiqul IslamView Answer on Stackoverflow
Solution 13 - asp.netSureshkumar TView Answer on Stackoverflow
Solution 14 - asp.netDmitriy PichuginView Answer on Stackoverflow
Solution 15 - asp.netSuresh kltView Answer on Stackoverflow
Solution 16 - asp.netAaronView Answer on Stackoverflow