Solved

how to compute sum using footer template with nested repeater

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

777 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