Results 1 to 17 of 17

Thread: Adding simple Math column in Gridview

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    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 %>" />

  2. #2
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    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?

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

    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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    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>

  5. #5
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    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";

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    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

  7. #7
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    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();
        }

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    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>

  9. #9
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    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

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

    Re: Adding simple Math column in Gridview

    Add an Imports statement to your page.

    Imports System.Data

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    Re: Adding simple Math column in Gridview

    Quote 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?

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

    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.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    Re: Adding simple Math column in Gridview

    Well that is all new to me?
    What do I do and where do I do it?

  14. #14
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    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

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

    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" %>

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Posts
    332

    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();
        }

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

    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
  •  



Click Here to Expand Forum to Full Width