Solved

how to compute sum using footer template with nested repeater

Posted on 2004-09-09
5
6,572 Views
Last Modified: 2012-08-13
I need to know how to compute sub-totals with nested repeaters using relationships between 2 datatables.   In reality the datatabless are from a database and there is no good way to compute them at the DB level.   I hope the following code snipit will demonstrate what I need.

Code follows:
=================================
<%@Page Language="vb"%>
<%@Import Namespace="System.Data" %>

<script language="vb" runat="server">

      Sub page_load(Sender As Object, E As EventArgs)

            Dim ds As New DataSet
        Dim dt As DataTable
        Dim dr As DataRow

            try
              dt = New DataTable("parent")

              dt.Columns.Add(New DataColumn("parent_id", GetType(String)))
              dt.Columns.Add(New DataColumn("parent_desc", GetType(String)))

            dr = dt.NewRow()
            dr(0) = "1"
            dr(1) = "parent 1"
            dt.Rows.Add(dr)

            dr = dt.NewRow()
            dr(0) = "2"
            dr(1) = "parent 2"
            dt.Rows.Add(dr)

            dr = dt.NewRow()
            dr(0) = "3"
            dr(1) = "parent 3"
            dt.Rows.Add(dr)

            ds.tables.add(dt)

              dt = New DataTable("child")

              dt.Columns.Add(New DataColumn("parent_id", GetType(String)))
              dt.Columns.Add(New DataColumn("child_desc", GetType(String)))
              dt.Columns.Add(New DataColumn("child_count", GetType(String)))

            dr = dt.NewRow()
            dr(0) = "1"
            dr(1) = " parent 1 child 1"
            dr(2) = "10"
            dt.Rows.Add(dr)

            dr = dt.NewRow()
            dr(0) = "1"
            dr(1) = " parent 1 child 2"
            dr(2) = "20"
            dt.Rows.Add(dr)

            dr = dt.NewRow()
            dr(0) = "2"
            dr(1) = " parent 2 child 1"
            dr(2) = "10"
            dt.Rows.Add(dr)

            dr = dt.NewRow()
            dr(0) = "2"
            dr(1) = " parent 2 child 2"
            dr(2) = "10"
            dt.Rows.Add(dr)

            dr = dt.NewRow()
            dr(0) = "3"
            dr(1) = " parent 3 child 1"
            dr(2) = "1"
            dt.Rows.Add(dr)

            ds.tables.add(dt)

                  ds.relations.add("myrelation", ds.tables("parent").columns("parent_id"), ds.tables("child").columns("parent_id"))

                  rptparent.Datasource = ds.tables("parent")
                  page.databind()

            catch ex as exception
                  response.write("<br>I messed up somewhere: " & ex.message)

            finally
                  ds.Dispose()
            end try
      end sub

</script>

<html>
      <form runat="server" id="f1">
            <asp:Repeater runat="server" id="rptParent">
                     <ItemTemplate>
                           <b><%# DataBinder.Eval(Container.DataItem, "parent_desc")%></b>
                        <asp:Repeater runat="server" id="rptChild"
                              datasource='<%# (cType(Container.DataItem,DataRowView)).Row.GetChildRows("myrelation") %> ' >

                                 <ItemTemplate>
                                       <li/><%# Container.DataItem("child_desc") %>&nbsp; Count: <%# Container.DataItem("child_count") %><br/>
                                 </ItemTemplate>

                              <FooterTemplate>
                                    <b>I want total child_count per parent here using something like: compute(sum(child_count),"")</b> <br/><br/><br/>
                              </FooterTemplate>

                        </asp:Repeater>      
                     </ItemTemplate>

            </asp:Repeater>      

      </form>
</html>



0
Comment
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:daffodils
ID: 12023829
Look at this article..

How to sum up a DataGrid column and have the sum displayed in the footer
http://aspnet.4guysfromrolla.com/articles/020503-1.aspx#postadlink
0
 
LVL 76

Author Comment

by:slightwv (䄆 Netminder)
ID: 12027207
I've seen many examples like the one posted in the link.  However none of them get it quite right.  I'm dealing with nested objects and need to compute sub-totals not totals over the whole object.  I have not been able to take any of the examples I've seen and get them to work.  

I'd like to see someone take my provided sample and make it work.  Hence the 500 points for the question.
0
 
LVL 33

Expert Comment

by:raterus
ID: 12027581
Are you wanting to Sum them or Count them?
0
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 12027816
Tada!

<%@ Page Language="vb" debug="true"%>
<%@ import Namespace="System.Data" %>
<script runat="server">

    Sub page_load(Sender As Object, E As EventArgs)

         Dim ds As New DataSet
       Dim dt As DataTable
       Dim dr As DataRow

         try
            dt = New DataTable("parent")

            dt.Columns.Add(New DataColumn("parent_id", GetType(String)))
            dt.Columns.Add(New DataColumn("parent_desc", GetType(String)))

           dr = dt.NewRow()
           dr(0) = "1"
           dr(1) = "parent 1"
           dt.Rows.Add(dr)

           dr = dt.NewRow()
           dr(0) = "2"
           dr(1) = "parent 2"
           dt.Rows.Add(dr)

           dr = dt.NewRow()
           dr(0) = "3"
           dr(1) = "parent 3"
           dt.Rows.Add(dr)

           ds.tables.add(dt)

            dt = New DataTable("child")

            dt.Columns.Add(New DataColumn("parent_id", GetType(String)))
            dt.Columns.Add(New DataColumn("child_desc", GetType(String)))
            dt.Columns.Add(New DataColumn("child_count", GetType(String)))

           dr = dt.NewRow()
           dr(0) = "1"
           dr(1) = " parent 1 child 1"
           dr(2) = "10"
           dt.Rows.Add(dr)

           dr = dt.NewRow()
           dr(0) = "1"
           dr(1) = " parent 1 child 2"
           dr(2) = "20"
           dt.Rows.Add(dr)

           dr = dt.NewRow()
           dr(0) = "2"
           dr(1) = " parent 2 child 1"
           dr(2) = "10"
           dt.Rows.Add(dr)

           dr = dt.NewRow()
           dr(0) = "2"
           dr(1) = " parent 2 child 2"
           dr(2) = "10"
           dt.Rows.Add(dr)

           dr = dt.NewRow()
           dr(0) = "3"
           dr(1) = " parent 3 child 1"
           dr(2) = "1"
           dt.Rows.Add(dr)

           ds.tables.add(dt)

              ds.relations.add("myrelation", ds.tables("parent").columns("parent_id"), ds.tables("child").columns("parent_id"))

              rptparent.Datasource = ds.tables("parent")
              page.databind()

         catch ex as exception
                throw
              response.write("<br>I messed up somewhere: " & ex.message)

         finally
              ds.Dispose()
         end try
    end sub

    Public Sub ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs)

        if e.Item.ItemType = ListItemType.Footer then
            Dim rpt as Repeater = DirectCast(sender, Repeater)
            Dim drows as DataRow() = DirectCast(rpt.datasource, DataRow())

            Dim count as integer = 0
            For each dr as DataRow in drows
                count += cint(dr(2))
            Next

            Dim lbl As Label = DirectCast(e.item.controls(1), Label)
            lbl.text = count.tostring

        end if

    End Sub

</script>
<html>
<head>
    <form id="f1" runat="server">
        <asp:Repeater id="rptParent" runat="server">
            <ItemTemplate>
                <b><%# DataBinder.Eval(Container.DataItem, "parent_desc")%></b>
                <asp:Repeater runat="server" id="rptChild" onitemdatabound="ItemDataBound" datasource='<%# (cType(Container.DataItem,DataRowView)).Row.GetChildRows("myrelation") %> ' >
                    <ItemTemplate>
                        <li />
                        <%# Container.DataItem("child_desc") %>&nbsp; Count: <%# Container.DataItem("child_count") %>
                        <br />
                    </ItemTemplate>
                    <FooterTemplate>
                        <b>Sum:</b> <asp:label id="lblCount" runat="server" /><br><br>
                    </FooterTemplate>
                </asp:Repeater>
            </ItemTemplate>
        </asp:Repeater>
    </form>
</head>
<body>
</body>
</html>
0
 
LVL 76

Author Comment

by:slightwv (䄆 Netminder)
ID: 12028264
raterus,

Thx for the solution.  I would have never gone that direction.  I was to busy trying to do it with datatable.compute.  I'm thinking it may not be the most efficient way to pull it off but at least it gives me a working model (I can always make it efficient later).

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 74
ASP.Net Session State alternatives 3 57
Calling stored proc in EDMX 11 23
Not showing JavaScript in the list 5 32
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now