|
-
May 13th, 2008, 09:28 AM
#1
Thread Starter
Hyperactive Member
[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]
-
May 13th, 2008, 02:51 PM
#2
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>
-
May 13th, 2008, 03:18 PM
#3
Thread Starter
Hyperactive Member
Re: Templatefield with label (adding asecending numbers)
-
May 13th, 2008, 03:37 PM
#4
Re: Templatefield with label (adding asecending numbers)
No problem, add resolved please.
-
May 13th, 2008, 03:37 PM
#5
Thread Starter
Hyperactive Member
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?
-
May 13th, 2008, 03:46 PM
#6
Re: Templatefield with label (adding asecending numbers)
Try
<HeaderStyle HorizontalAlign="Center" />
(Just before the <HeaderTemplate>)
-
May 14th, 2008, 09:09 AM
#7
Thread Starter
Hyperactive Member
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?
-
May 14th, 2008, 09:27 AM
#8
Re: Templatefield with label (adding asecending numbers)
 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>
-
May 14th, 2008, 08:31 PM
#9
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|