trying to display header only

Discussion in 'ASP .Net Datagrid Control' started by David Cho, Apr 30, 2005.

  1. David Cho

    David Cho Guest

    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 ***
    David Cho, Apr 30, 2005
    #1
    1. Advertising

  2. There is no need to use TemplateColumns. Use BoundColumns.

    Eliyahu

    "David Cho" <> wrote in message
    news:...
    > 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 ***
    Eliyahu Goldin, May 1, 2005
    #2
    1. Advertising

  3. David Cho

    David Cho Guest

    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 ***
    David Cho, May 2, 2005
    #3
  4. 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

    "David Cho" <> wrote in message
    news:...
    > 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 ***
    Eliyahu Goldin, May 2, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    4
    Views:
    701
    lilburne
    Nov 2, 2003
  2. andy6 via DotNetMonster.com

    Trying to perform build...website will only display debug

    andy6 via DotNetMonster.com, Jun 2, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    416
    andy6 via DotNetMonster.com
    Jun 5, 2006
  3. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    603
    Jack Klein
    Jul 24, 2004
  4. mlt
    Replies:
    2
    Views:
    831
    Jean-Marc Bourguet
    Jan 31, 2009
  5. Steve
    Replies:
    0
    Views:
    144
    Steve
    Apr 22, 2008
Loading...

Share This Page