trying to display header only

D

David Cho

I have a form with a datagrid control. And I have a button that will
retrieve data and upon postback, the datagrid gets populated.

What I would like to have is, upon the loading of the form (before
postback), have the datagrid display the header with all the column
headings.

Right now, nothing is visible and the heading does not show unless the
DataSource has at least one row.

I tried the following.

<asp:datagrid id="dgEmployees" ShowFooter="False"
AutoGenerateColumns="False" HorizontalAlign="Center"
BackColor="#eeeeee" Runat="server" DataKeyField="EmployeeID">
<Columns>
<asp:TemplateColumn HeaderText="Last Name">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "LastName")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="First Name">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "FirstName")%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

//code behind
dgEmployees.DataSource = null;
dgEmployees.DataBind();
 
D

David Cho

Eliyahu,

Well, using BoundColumn as opposed to TemplateColumn does not display
the columns when the DataSource has no records. Did you understand what
I was trying to accomplish here?
 
E

Eliyahu Goldin

Yes, I did. And the headers are displayed perfectly regardless of the number
of records in the datasource. The typical scenario is when you run a query,
populate the dataset and databind the grid. If the query returns an empty
result set, you will still see the headers. I have this scenario working in
tens of pages. In your code you set DataSource to null. I don't believe it's
the reason for your problem, but it does look a bit odd.

Eliyahu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top