Solved

how to compute sum using footer template with nested repeater

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FInd Image Control Gridview 3 35
Adding items to a C# list incrementally 5 60
Aspx calendar pop up control 3 28
YouTube API get the Video Tags - vb.net 7 30
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

749 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