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:
<asp:SqlDataSource ID="SqlDS_TransactionDetail" runat="server"
ConnectionString="<%$ ConnectionStrings:ArrowConnectionString %>"
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))">
<SelectParameters>
<asp:Parameter Name="CRM_GUID" Type="String" />
<asp:Parameter Name="REF_ARROW" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<table>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDS_TransactionDetail">
<HeaderTemplate>
<tr>
<td>
NET_VALUE
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("NET_VALUE")%>
<%intNetValueTotal = Eval("NET_VALUE") + Eval("NET_VALUE")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td>
Total: <%=intNetValueTotal%>
</td>
</tr>
</FooterTemplate>
</asp:Repeater>
</table>
Does anyone understand what I am trying to achieve? :rolleyes:
Help!!!!
Cheers,
Scott
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
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:
Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
If (e.Item.ItemType = ListItemType.Item) Or _
(e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim newItem As Data.DataRowView
newItem = e.Item.DataItem
For intThisItem = 0 To newItem.Row.ItemArray.Length
If intThisItem = 2 Then
intNetValueTotal = intNetValueTotal + newItem.Row.ItemArray(3)
End If
Next
End If
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
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
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.