|
-
May 14th, 2008, 09:01 AM
#1
Thread Starter
Hyperactive Member
Adding simple Math column in Gridview
I am looking to add a simple math column in Gridview.
basically take the results of Column2 and divide by the number in Column1.
I think this should be simple but I have not found the info searching around.
Column1 = punt_ret_no
Column2 = punt_ret_yds
Column2 = [punt_ret_yds] / [punt_ret_no]
Just for the thread here are my coumns
Code:
<asp:GridView ID="GridView1" AllowSorting="true" runat="server" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound"
SkinID="booksSkin" AutoGenerateColumns="False" HorizontalAlign="Left">
<Columns>
<asp:BoundField HeaderText="Punt Returns/gm" DataField="punt_ret_no" ItemStyle-HorizontalAlign="Center"/>
<asp:BoundField HeaderText="Return Yards/gm" DataField="punt_ret_yds" ItemStyle-HorizontalAlign="Center"/>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [punt_ret_no], [punt_ret_yds] FROM [team_avg] ORDER BY [punt_ret_yds] DESC"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" />
-
May 14th, 2008, 09:58 AM
#2
Frenzied Member
Re: Adding simple Math column in Gridview
so you want column2 to be overwritten with the formula result or are you adding a third column?
-
May 14th, 2008, 01:30 PM
#3
Re: Adding simple Math column in Gridview
Create new TemplateField column. Add an ItemTemplate to it. Put a label in there.
In the codebehind, handle the rowdatabound event. Get the value of punt_ret_no, get the value of punt_ret_yds, divide. Get the label from the template field. Set the label's text property to that result of your division.
-
May 16th, 2008, 12:05 PM
#4
Thread Starter
Hyperactive Member
Re: Adding simple Math column in Gridview
Here is what I got and I don't think I am too far off. But I am getting a compilation error. Can anyone see any glaring error I have here?
Compiler Error Message: CS0246: The type or namespace name 'DataTable' could not be found (are you missing a using directive or an assembly reference?)
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 Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Average"));
dt.Columns["Average"].Expression = "Return Yards/Punt Returns";
GridView1.DataSource = dt;
GridView1.DataBind();
}
</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="Center">
<HeaderStyle HorizontalAlign="Center" />
<Columns>
<asp:BoundField HeaderText="Team" DataField="team" ItemStyle-HorizontalAlign="Center"/>
<asp:BoundField HeaderText="Conf" DataField="team_conf" ItemStyle-HorizontalAlign="Center"/>
<asp:BoundField HeaderText="Punt Returns" DataField="punt_ret_no" ItemStyle-HorizontalAlign="Center"/>
<asp:BoundField HeaderText="Return Yards" DataField="punt_ret_yds" ItemStyle-HorizontalAlign="Center"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Average") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [team], [team_conf], [punt_ret_no], [punt_ret_yds], [punt_ret_td] FROM [team_avg] ORDER BY [punt_ret_yds] DESC"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" />
</div>
</asp:Content>
-
May 16th, 2008, 12:24 PM
#5
Re: Adding simple Math column in Gridview
Code:
dt.Columns["Average"].Expression = "Return Yards/Punt Returns";
should be
Code:
dt.Columns["Average"].Expression = "punt_ret_yds/punt_ret_no";
-
May 16th, 2008, 02:33 PM
#6
Thread Starter
Hyperactive Member
Re: Adding simple Math column in Gridview
I actually tried that and got the same error.
It acts as if I am getting failure before it gets to that point.
I am getting the "blue squiggly" lines under
Bold code below.
Code:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn
-
May 16th, 2008, 02:40 PM
#7
Re: Adding simple Math column in Gridview
before that where is team,punt_ret_yds,punt_ret_no etc coulmns in your datatable that you are binding with gridview ?also there should be a check of IsPostback !
Code:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Average"));
dt.Columns["Average"].Expression = "Return Yards/Punt Returns";
GridView1.DataSource = dt;
GridView1.DataBind();
}
-
May 16th, 2008, 03:14 PM
#8
Thread Starter
Hyperactive Member
Re: Adding simple Math column in Gridview
Thank you for the help on this.
However, I am confused on your question. [team],etc..are all coming from Gridview1 that is called from the SQL Server.
Below is my entire code that I am using for this page.
I am wondering if my ItemTemplates are getting confused with each other some how?
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 + 1).ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Average"));
dt.Columns["Average"].Expression = "punt_ret_yds/punt_ret_no";
GridView1.DataSource = dt;
GridView1.DataBind();
}
</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="Center">
<HeaderStyle HorizontalAlign="Center" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Rank
</HeaderTemplate>
<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="Conf" DataField="team_conf" ItemStyle-HorizontalAlign="Center"/>
<asp:BoundField HeaderText="Punt Returns" DataField="punt_ret_no" ItemStyle-HorizontalAlign="Center"/>
<asp:BoundField HeaderText="Return Yards" DataField="punt_ret_yds" ItemStyle-HorizontalAlign="Center"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Average") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [team], [team_conf], [punt_ret_no], [punt_ret_yds], [punt_ret_td] FROM [team_avg] ORDER BY [punt_ret_yds] DESC"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" />
</div>
</asp:Content>
-
May 16th, 2008, 03:29 PM
#9
Re: Adding simple Math column in Gridview
first you are binding gridview as
<asp:GridView ID="GridView1" AllowSorting="true" runat="server" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound"
but in page load again u r overriding this with
GridView1.DataSource = dt;
where dt has only one column in it
-
May 16th, 2008, 03:32 PM
#10
Re: Adding simple Math column in Gridview
Add an Imports statement to your page.
Imports System.Data
-
May 16th, 2008, 04:09 PM
#11
Thread Starter
Hyperactive Member
Re: Adding simple Math column in Gridview
 Originally Posted by riteshjain1982
first you are binding gridview as
<asp:GridView ID="GridView1" AllowSorting="true" runat="server" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound"
but in page load again u r overriding this with
GridView1.DataSource = dt;
where dt has only one column in it
OK, I understand this point.
am I better off having this code handle the entire gridview and eliminate my gridview1 as it is now? Or can I just redirect it so it is not overriding it?
Mendhak....
Is that a solution to what I have now or do I need to clear the above problem first?
-
May 16th, 2008, 04:26 PM
#12
Re: Adding simple Math column in Gridview
Well, based upon your error:
The type or namespace name 'DataTable' could not be found (are you missing a using directive or an assembly reference?)
It means you're missing a reference. If that's what all the to-and-fro is about, then the Imports should fix it.
-
May 16th, 2008, 05:08 PM
#13
Thread Starter
Hyperactive Member
Re: Adding simple Math column in Gridview
Well that is all new to me?
What do I do and where do I do it?
-
May 16th, 2008, 06:14 PM
#14
Frenzied Member
Re: Adding simple Math column in Gridview
not sure how you do it in C# but in VB it's added at the top of your class:
Imports System.Data
-
May 17th, 2008, 04:41 AM
#15
Re: Adding simple Math column in Gridview
Because you're using inline ASP.NET code, you'd need to place this under the Page directive.
<%@ import namespace="System.Data" %>
-
May 21st, 2008, 11:50 AM
#16
Thread Starter
Hyperactive Member
Re: Adding simple Math column in Gridview
Mendhak,
I got that part entered and now I am getting a syntax error?
"Syntax error: Missing operand after 'Yards' operator."}
Here is my code behind.
I am wondering do I need to use the actual SQL column heading's? or my Gridview heading's? I am using the gridview see below.
Code:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Average"));
dt.Columns["Average"].Expression = "Return Yards/Punt Returns";
GridView1.DataSource = dt;
GridView1.DataBind();
}
-
May 21st, 2008, 03:39 PM
#17
Re: Adding simple Math column in Gridview
It needs to be the actual SQL column heading.
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
|