views:

14213

answers:

8

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.

+6  A: 

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;
}
Joshua Hudson
It's a kludgy mess, but thanks for following up with your solution. +1
John MacIntyre
What can I do if the datasource is a List<>?
Sauron
Too easy! Thanks! +1
Javaman59
A: 

EDIT: nevermind

No this is not a bad solution. You could place this code on the on_Bind event for the grid.

or as the poster above said, you could create a header template and "hard code" your header text.

Victor
No, doing so will render a blank row with nothing in it (If borders are on you would see them). We only want the headers to show.
Joshua Hudson
yeah, i missed the line where you were adding an empty row.
Victor
+3  A: 

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.

Liwen
A: 

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>
I tried this solution but it doesn't show me the headers :(
LuRsT
A: 

this good code in codeproject.com http://www.codeproject.com/KB/aspnet/Fix_empty_GridView_issue.aspx

and it's solved this issue

+3  A: 

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.

StriplingWarrior
+1  A: 

please check this post i think i get it : http://ledomoon.blogspot.com/2009/04/show-grid-view-header-and-footer-when.html

Waleed Mohamed
A: 

hi thanks, nice morvellous. becouse other site provive very rough headic soln

surendra singh kushwaha
Please post comments like this to the actual solution and not as an answer.
Joshua Hudson
rofl that guy must have been drinking a lot.
Yves M.