Results 1 to 9 of 9

Thread: [RESOLVED] Templatefield with label (adding asecending numbers)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    [RESOLVED] Templatefield with label (adding asecending numbers)

    Please see the below link.
    Notice the far left column does not have text in the header and also notice that the ranking starts with number "0". Rather than "1".

    If anyone can see my code and see where I went wrong I would be greatly appreciated.

    I would like the header to have the work "Rank" in the fist row of the first column.

    http://visualboxscore.com/cfb/team_s...l_offense.aspx

    Code:
    <%@ Page Language="C#" MasterPageFile="~/AppMaster.master" Title="visualboxscore - CFB Total Offense" %>
    <asp:Content ID="Content1" runat="server" ContentPlaceHolderID="mainCopy">
    <script runat="server">
    
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
    
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
    
                int row = e.Row.RowIndex + (GridView1.PageSize * GridView1.PageIndex);
    
                ((Label)e.Row.FindControl("Rank")).Text = row.ToString();
    
    
            } 
        }
    
    </script>
    <div class="container" style="height: 80%">
        <br>
        <asp:GridView ID="GridView1" AllowSorting="true" runat="server" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound"
          SkinID="booksSkin" AutoGenerateColumns="False" HorizontalAlign="Left">
          <Columns>
            <asp:TemplateField>
                <itemtemplate>
                    <asp:Label ID="Rank" runat="server" Text="Rank"></asp:Label>
                </itemtemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Team" DataField="team" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField HeaderText="Plays/Gm" DataField="total_plays" ItemStyle-HorizontalAlign="Center" SortExpression="total_plays" />
            <asp:BoundField HeaderText="Total Yards" DataField="total_yds" ItemStyle-HorizontalAlign="Center" SortExpression="total_yds" />
            <asp:BoundField HeaderText="YPP" DataField="yds_per_play" ItemStyle-HorizontalAlign="Center" SortExpression="yds_per_play" />
          </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
          SelectCommand="SELECT [team], [total_plays], [total_yds], [yds_per_play], [punt_no], [total_first_downs] FROM [team_avg] ORDER BY [total_yds] DESC"
           ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" />
    </div>
    </asp:Content>
    Last edited by ggodwin; May 14th, 2008 at 08:31 PM. Reason: [RESOLVED]

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Templatefield with label (adding asecending numbers)

    Because .NET is 0 based, you need to increment it by +1.

    Code:
     ((Label)e.Row.FindControl("Rank")).Text = (row+1).ToString();
    To get "Rank" into your header, use the HeaderTemplate.

    Code:
    <asp:TemplateField>
                <HeaderTemplate>
                    Rank
                </HeaderTemplate>
                <itemtemplate>
                    <asp:Label ID="Rank" runat="server" Text="Rank"></asp:Label>
                </itemtemplate>
            </asp:TemplateField>

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    Re: Templatefield with label (adding asecending numbers)

    Thank you!!!!

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Templatefield with label (adding asecending numbers)

    No problem, add resolved please.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    Re: Templatefield with label (adding asecending numbers)

    What would be the best way to get the text in the header to align in the center?

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Templatefield with label (adding asecending numbers)

    Try
    <HeaderStyle HorizontalAlign="Center" />

    (Just before the <HeaderTemplate>)

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    Re: Templatefield with label (adding asecending numbers)

    menhak,
    Does this command work for the entire header? or just the column that has the cell that is using the header template?

    I am assuming I should change all my headers to follow this same command. Does this question make sense?

  8. #8
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Templatefield with label (adding asecending numbers)

    Quote Originally Posted by ggodwin
    menhak,
    Does this command work for the entire header? or just the column that has the cell that is using the header template?

    I am assuming I should change all my headers to follow this same command. Does this question make sense?
    That command will work for this particular column only. In order to add effect to all columns, add <HeaderStyle> just before you start <Column> nodes.
    HTML Code:
    <asp:GridView id="something" runat="server" ...>
    
    <HeaderStyle HorizontalAlign="Center" />
    
    <Columns>
    ...
    </Columns>
    </asp:GridView>
    Show Appreciation. Rate Posts.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    Re: [RESOLVED] Templatefield with label (adding asecending numbers)

    Thanks Harsh this thread is resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width