Solved

how to compute sum using footer template with nested repeater

Posted on 2004-09-09
5
6,547 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

19 Experts available now in Live!

Get 1:1 Help Now