Solved

how to compute sum using footer template with nested repeater

Posted on 2004-09-09
5
6,676 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 77

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 77

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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