Home All Groups Group Topic Archive Search About
Author
29 Apr 2005 11:53 PM
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();

*** Sent via Developersdex http://www.developersdex.com ***

Author
1 May 2005 10:04 AM
Eliyahu Goldin
There is no need to use TemplateColumns. Use BoundColumns.

Eliyahu

Show quoteHide quote
"David Cho" <profnac***@yahoo.com> wrote in message
news:eZeHaaRTFHA.544@TK2MSFTNGP15.phx.gbl...
> 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();
>
> *** Sent via Developersdex http://www.developersdex.com ***
Author
2 May 2005 4:57 PM
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?

*** Sent via Developersdex http://www.developersdex.com ***
Author
2 May 2005 7:35 PM
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

Show quoteHide quote
"David Cho" <profnac***@yahoo.com> wrote in message
news:eKybNgzTFHA.1148@tk2msftngp13.phx.gbl...
> 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?
>
> *** Sent via Developersdex http://www.developersdex.com ***