Results 1 to 5 of 5

Thread: How do I add values together from my dataset (with a repeater)?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    How do I add values together from my dataset (with a repeater)?

    Alright, second blonde moment for the day!

    I've figured out how to use a repeater field, now what I want to achieve is a "total" figure for the column, and this figure needs to appear the footer.

    Back in ASP 3.0, I would simply create a variable, and on every step as I looped through the recordset, I would add the current to the variable, including its original value. At the end, I'd have the total. Apparently I can't figure out how to do the same thing in ASP.NET.

    This is what I'm doing (which doesn't work):

    VB Code:
    1. <asp:SqlDataSource ID="SqlDS_TransactionDetail" runat="server"
    2.         ConnectionString="<%$ ConnectionStrings:ArrowConnectionString %>"
    3.        
    4.         SelectCommand="SELECT [QUANTITY], [DESCRIPTION], [SELLING_PRICE], [NET_VALUE], [TAX_VALUE] FROM [ARROW_TO_CRM_DEBTOR_CHECKDETAIL] WHERE (([CRM_GUID] = @CRM_GUID) AND ([REF_ARROW] = @REF_ARROW))">
    5.         <SelectParameters>
    6.             <asp:Parameter Name="CRM_GUID" Type="String" />
    7.             <asp:Parameter Name="REF_ARROW" Type="String" />
    8.         </SelectParameters>
    9.     </asp:SqlDataSource>
    10.     <table>
    11.     <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDS_TransactionDetail">
    12.         <HeaderTemplate>
    13.         <tr>  
    14.         <td>
    15.         NET_VALUE
    16.         </td>
    17.         </tr>
    18.         </HeaderTemplate>
    19.         <ItemTemplate>
    20.         <tr>  
    21.         <td>
    22.         <%#Eval("NET_VALUE")%>
    23.         <%intNetValueTotal = Eval("NET_VALUE") + Eval("NET_VALUE")%>
    24.         </td>
    25.         </tr>
    26.         </ItemTemplate>
    27.         <FooterTemplate>
    28.         <tr>
    29.         <td>
    30.         Total: <%=intNetValueTotal%>
    31.         </td>
    32.         </tr>
    33.         </FooterTemplate>
    34.     </asp:Repeater>
    35.     </table>

    Does anyone understand what I am trying to achieve?

    Help!!!!

    Cheers,
    Scott

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How do I add values together from my dataset (with a repeater)?

    Hey,

    As a general approach...

    Use the ItemDataBound event of the Repeater:

    http://msdn.microsoft.com/en-us/libr...databound.aspx

    This happens each time an Item is bound, check to see what type of Item it is, if it is the one you are after, i.e. not the header or footer, grab the information you need, add it to your variable, and then use this variable when the footer item gets bound.

    Hope that helps!

    Gary

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Re: How do I add values together from my dataset (with a repeater)?

    Hey Gary,

    Thanks for the help and the reference to the ItemDataBound event.

    I managed to get the job done, mind you, it doesn't seem overly efficient!

    vb Code:
    1. Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
    2.  
    3.         If (e.Item.ItemType = ListItemType.Item) Or _
    4.                     (e.Item.ItemType = ListItemType.AlternatingItem) Then
    5.  
    6.             Dim newItem As Data.DataRowView
    7.             newItem = e.Item.DataItem
    8.             For intThisItem = 0 To newItem.Row.ItemArray.Length
    9.                 If intThisItem = 2 Then
    10.                     intNetValueTotal = intNetValueTotal + newItem.Row.ItemArray(3)
    11.                 End If
    12.             Next
    13.         End If
    14.  
    15.     End Sub

    If you have any recommendations on how to make this work better, feel free to chime in. If not, thanks again the help!

    Cheers,
    Scott

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How do I add values together from my dataset (with a repeater)?

    Hey,

    On the whole, it looks good.

    The only thing I would suggest in addition would suggest to do differently would be to search directly for the control that you are interested in, rather than use the For loop.

    Have a look at the following:

    http://msdn.microsoft.com/en-us/libr...ndcontrol.aspx

    Use that to grab the control that you want.

    Hope that helps!

    Gary

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: How do I add values together from my dataset (with a repeater)?

    I agree that it isn't the best ay to do this. If you were doing this via a dataset or a business logic layer, this would be simpler (and more straightforward) because I'd then tell you to loop through the dataset and add the total up. After all, it's just a simple value and all you need to do is place it at the bottom of the grid or repeater, but here you're depending on UI functionality to add the numbers up.

    However, to do it the proper way would involve you getting rid of the SqlDataSource which you will consider to be more effort than it's worth.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width