Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView - Show headers on empty data source

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. :)

like image 754
Joshua Hudson Avatar asked Dec 09 '08 21:12

Joshua Hudson


People also ask

How to show header in GridView when no data?

GridView has a property called EmptyDataText which shows the desired message when the GridView is empty. But it displays the message but does not display the header. One way is to check whether there are records returned from database or not and based on that create an empty dataset and bind it to GridView.

How can show empty GridView with header in asp net?

To do that, we need to enable the Boolean property ShowHeaderWhenEmpty to True. Be sure you're using the ASP.NET 4.0 or later version to use this property. Also, we need to add the <EmptyDataTemplate></EmptyDataTemplate> property inside the grid view to display the message.

How do I make GridView header always visible?

Just go inside the code of GridView and add a style tag with below properties. You can change overflow:auto to overflow:scroll but that will show both Scrollbar and always.

How do I check if GridView is empty?

You should check this by directly querying the data binding source of the gridview (see if the actual list that's grid view bound to is empty or not). Show activity on this post. Hi @Canavar, just wanted to know if GridView. DataBind(); is possible without binding to any datasource ?


3 Answers

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()
like image 122
zacharydl Avatar answered Oct 19 '22 11:10

zacharydl


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;
}
like image 24
Joshua Hudson Avatar answered Oct 19 '22 12:10

Joshua Hudson


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.

like image 27
StriplingWarrior Avatar answered Oct 19 '22 12:10

StriplingWarrior