Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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: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 23-Nov-13 8:26am
iratus7256
Edited 23-Nov-13 8:27am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

HI Try this sample code..
 
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);
            }
        }
 

    }
 

}
 

 

 

 

<%@ 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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

CodeProject Frequently Asked Questions Series 1: The ASP.NET GridView[^] could give you some idea on how to achieve this.
  Permalink  
Comments
iratus7 at 24-Nov-13 8:25am
   
Very usefull
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I finally make two views in my sql and bind them using a repeater inside the gridview using this:
 
<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>
<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>
<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>
<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
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.150129.1 | Last Updated 24 Nov 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100