Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello again.I have three columns.In these columns the values are like: 3/4 ,5/6 etc.I want to show in the equivalent footer cell the sum and divide of this columns. Example if a column has 2 rows with values 1/2 , 2/4 I want to show 1+2/2+4=0.5=50% in the footer cell .Any suggestions?

this is the asp code from the GridViewHomeTeamStats that are included :

ASP.NET
<asp:GridView ID="GridViewHomeTeamStats" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceHomeTeam" ShowFooter="True" OnRowDataBound="GridViewHomeTeamStats_RowDataBound">
    <Columns>            
        <asp:TemplateField HeaderText="FT" SortExpression="FREE_THROW_MADE">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("FREE_THROW_MADE") + "/" + Eval("FREE_THROW_ATTEMPT") %>'></asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="2P" SortExpression="2POINTS_MADE">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("2POINTS_MADE") + "/" + Eval("2POINTS_ATTEMPT") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="3P" SortExpression="3POINT_MADE">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("3POINT_MADE") + "/" + Eval("3POINT_ATTEMPT") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
Posted
Updated 23-Nov-13 7:27am
v2

HI Try this sample code..

C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) { }
            else
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("FREE_THROW_MADE", typeof(double));
                dt.Columns.Add("FREE_THROW_ATTEMPT", typeof(double));
                dt.Rows.Add(1, 2);
                dt.Rows.Add(2, 4);
                GridViewHomeTeamStats.DataSource = dt;
                GridViewHomeTeamStats.DataBind();
            }
        }

        double avg = 0;
        int count = 0;


        protected void GridViewHomeTeamStats_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                count++;
                var text = (e.Row.FindControl("Label1") as Label).Text.Trim();
                if (text.Contains(@"/"))
                {
                    var arytemp = text.Split('/');
                    if (arytemp.Length == 2)
                    {
                        double FREE_THROW_MADE =  Convert.ToDouble( arytemp[0]);
                        double FREE_THROW_ATTEMPT = Convert.ToDouble(arytemp[1]);
                        avg += (FREE_THROW_MADE / FREE_THROW_ATTEMPT);
                    }
                }

            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = string.Format("{0} %", (avg * 100)/count);
            }
        }


    }


}









XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery.js.js"></script>
    <script type="text/javascript">

        var openchild = function () {

            window.open('childpage.aspx', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');
            return false;
        }


    </script>


</head>
<body>
    <form id="form1" runat="server">

        <asp:GridView ID="GridViewHomeTeamStats" runat="server" AutoGenerateColumns="False"  ShowFooter="True" OnRowDataBound="GridViewHomeTeamStats_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="FT" SortExpression="FREE_THROW_MADE">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("FREE_THROW_MADE") + "/" + Eval("FREE_THROW_ATTEMPT") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
        </asp:GridView>


    </form>
</body>
</html>
 
Share this answer
 
CodeProject Frequently Asked Questions Series 1: The ASP.NET GridView[^] could give you some idea on how to achieve this.
 
Share this answer
 
Comments
iratus7 24-Nov-13 8:25am    
Very usefull
I finally make two views in my sql and bind them using a repeater inside the gridview using this:

ASP.NET
<asp:templatefield headertext="FT" sortexpression="FREE_THROW_MADE" xmlns:asp="#unknown">
                            <itemtemplate>
                                <asp:label id="Label1" runat="server" text="<%# Eval("FREE_THROW_MADE") + "/" + Eval("FREE_THROW_ATTEMPT") %>"></asp:label>
                            </itemtemplate>
                            <footertemplate> <asp:repeater id="Repeater1p" runat="server" datasourceid="SqlDataSourceHomePercentage"> <itemtemplate><asp:label id="Label1P" runat="server" text="<%# Eval("FREE_THROW_MADE_SUM") + "/" + Eval("FREE_THROW_ATTEMPT_SUM") %>"></asp:label><br /><asp:label id="FtPer" runat="server" text="<%# Eval("[FT_PERCENTAGE]","{0:P1}")%>"></asp:label></itemtemplate></asp:repeater>
                            </footertemplate>
                        </asp:templatefield>
                        <asp:templatefield headertext="2P" sortexpression="2POINTS_MADE" xmlns:asp="#unknown">
                            <itemtemplate>
                                <asp:label id="Label2" runat="server" text="<%# Eval("2POINTS_MADE") + "/" + Eval("2POINTS_ATTEMPT") %>"></asp:label>
                            </itemtemplate>
                            <footertemplate> <asp:repeater id="Repeater2p" runat="server" datasourceid="SqlDataSourceHomePercentage"> <itemtemplate><asp:label id="Label2P" runat="server" text="<%# Eval("2POINTS_MADE_SUM") + "/" + Eval("2POINTS_ATTEMPT_SUM") %>"></asp:label><br /><asp:label id="TowpPer" runat="server" text="<%# Eval("[2P_PERCENTAGE]","{0:P1}")%>"></asp:label></itemtemplate></asp:repeater>
                            </footertemplate>
                        </asp:templatefield>
                        <asp:templatefield headertext="3P" sortexpression="3POINT_MADE" xmlns:asp="#unknown">
                            <itemtemplate>
                                <asp:label id="Label3" runat="server" text="<%# Eval("3POINT_MADE") + "/" + Eval("3POINT_ATTEMPT") %>"></asp:label>
                            </itemtemplate>
                            <footertemplate> <asp:repeater id="Repeater3p" runat="server" datasourceid="SqlDataSourceHomePercentage"> <itemtemplate><asp:label id="Label3P" runat="server" text="<%# Eval("3POINT_MADE_SUM") + "/" + Eval("3POINT_ATTEMPT_SUM") %>"></asp:label><br /><asp:label id="ThreepPer" runat="server" text="<%# Eval("[3P_PERCENTAGE]","{0:P1}")%>"></asp:label></itemtemplate></asp:repeater>
                            </footertemplate>
                        </asp:templatefield>



I think karthik mahalingam01's solution is more profesional
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900