Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Column total in data repeater 2

Status
Not open for further replies.

arst06d

Programmer
Nov 29, 2002
324
Hi

I have a repeater bound to an xmldatasource.

Is there a way I can calculate & display the sum of a column, preferably in the <footertemplate> of the repeater, possibly by interrogating the values as the control is rendered?

If it's not possible in the repeater itself, can I interrogate the control before the page itself is rendered, cycle through the rows and calculate the total? - to be displayed in a control outside the repeater.

Thanks

David
 
Try using the ItemDataBound event of the repeater to keep a running total


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
OK, here's what I've done:

After the databind I call the following proc:
Code:
   Protected Sub calcTotal()
        Dim sum As Integer = 0
        Dim i As Integer
        For i = 0 To rptPayments.Items.Count - 1
            If (rptPayments.Items(i).ItemType = ListItemType.Item) Or (rptPayments.Items(i).ItemType = ListItemType.AlternatingItem) Then
                sum += CType(rptPayments.Items(i).FindControl("litAmount"), Literal).Text
            End If
        Next

        Me.litTotal.InnerText = sum
    End Sub

the litTotal item is a control outside the repeater templates. I tried to put it in the footertemplate but I couldn't isolate the listitemtype.footertemplate when cycling the items collection - possibly as there was no databinding in the footer?

Anyway - that's my solution.
 
There is no reason to code a proc and run it after the databind. Do the code while the data is binding as Ca8msm has suggested. If you use the ItemDataBound event you can do the summing. Also you can check the itemtype for each row. You sum if it is an Item or Alternating item. Then once you hit the footer row, you display your total.

Jim
 
Alternatively, rather than looping through the Repeater, you could use the ItemDataBound event like I suggested above.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Sorry, was posting at the same time Jim.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks all - here's the final code:

Code:
    Protected Sub rptPayments_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPayments.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            dblSumPayments = dblSumPayments + CType(e.Item.FindControl("litAmount"), Literal).Text
        End If
        If e.Item.ItemType = ListItemType.Footer Then
            CType(e.Item.FindControl("litAmount"), Literal).Text = dblSumPayments
        End If
    End Sub
 

OK - an added twist to this.
Users how decide they want the total at the top of the column.
Code now reads:
Code:
    Protected Sub rptPayments_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPayments.ItemDataBound
        'calculate the total of all the payments and display in the footer
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            dblSumPayments = dblSumPayments + CType(e.Item.FindControl("litAmount"), Literal).Text
        End If
        If e.Item.ItemType = ListItemType.Footer Then
            'display in the footer, and the header
            CType(e.Item.FindControl("litAmount"), Literal).Text = dblSumPayments

            Dim item As RepeaterItem
            For Each item In rptPayments.Items
                If item.ItemType = ListItemType.Header Then
                    CType(item.FindControl("litAmountHeader"), Literal).Text = dblSumPayments
                End If
            Next


        End If
    End Sub

However - iterating through the items does not find the header template, so the total is not displayed.

Anyone know how I can isolate the header template?

Thanks
 
OK
Here's how I did it.

Apparently the RepeaterItems collection only returns databound items. To grab the header you have to use the controls collection.

The first control in the repeater is the datatable.
The first control in the datatable is the header.

so my code becomes:

Code:
    Protected Sub rptPayments_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPayments.ItemDataBound
        'calculate the total of all the payments and display in the footer
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            dblSumPayments = dblSumPayments + CType(e.Item.FindControl("litAmount"), Literal).Text
        End If
        If e.Item.ItemType = ListItemType.Footer Then
            'display in the footer, and the header
            CType(e.Item.FindControl("litAmount"), Literal).Text = dblSumPayments

            CType(rptPayments.Controls(0).Controls(0).FindControl("litAmountHeader"), Literal).Text = dblSumPayments

        End If
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top