Home
ASP.Net Repeater Column Total
Blog Date 11 June 2021
The HTML
<table class="TableStyle1">
<tr>
<th>Invoice Job
</th>
<th>Invoice Parts
</th>
<th>Invoice Customer
</th>
<th>Date
</th>
<th>Price
</th>
</tr>
<asp:Repeater ID="MyGrid" OnItemDataBound="MyGrid_ItemDataBound" runat="server">
<ItemTemplate>
<tr>
<td>
<%# Eval("InvoiceJob") %>
</td>
<td>
<%# Eval("InvoicePart") %>
</td>
<td>
<%# Eval("InvoiceCustName") %>
</td>
<td>
<%# RC.FixDate(Convert.ToString(Eval("InvoiceDate"))) %>
</td>
<td>£<asp:Label ID="InvoicePrice" Text='<%# Eval("InvoicePrice") %>' runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<tr>
<td>Total
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>£<asp:Literal ID="PriceTotal" runat="server" />
</td>
</tr>
</table>
The Code
MyGrid.DataSource = SOME-KINDA-SQL-OR-DATATABLE
MyGrid.DataBind();
double MyTotal = 0;
foreach(RepeaterItem MyItem in MyGrid.Items)
{
Label InvoicePrice = MyItem.FindControl("InvoicePrice") as Label;
MyTotal = MyTotal + Convert.ToDouble(InvoicePrice.Text);
}
PriceTotal.Text = MyTotal.ToString();
}
The Explanation
This is yet another one of those "URGH! FFS why do people make things so complicated..."
I got buried in a mound of onItemDataBound and onBound this and that and the other. Endless line after line of complex code in voids with objects and vague vars and multi-layered functions within functions. It need not be so complex, mercifully.
Point 1. The column you wish to add up, to sum up, to totalise, you can't simply use the old...
<%# Eval("ColumnName") %>
You need to give ASP.Net something to grab onto, to FIND. A textbox made to look like regular text would work but is rather unneccessary. The "Label" tag will be fine and requires no additional formatting. Note when using Label or Textbox with the Eval function then don't use double quotes, just single. Why? Errrrrr... dunno
<asp:Label ID="ColumnName" Text='<%# Eval("ColumnName") %>' runat="server" />
Now that ASP.Net has something it can get a handle on, where to put the code to get it an add it up? As mentioned there's all kinds of onThis and onThat but here we save the effort and place our code AFTER getting the data and binding it to the repeater.
You'll also need a variable, in this case a number type variable (Int32, Int64, double, decimal etc) that you can use to add the values into.
Int32 MyTotal = 0;
So after binding we use a foreach for, errr, each of the repeater's items. At a guess an item is a row, the equivalent of a DataTable's DataRow. So we loop through each row in the repeater now the repeater has been bound - aka filled up with data.
We then use FindControl to, well, find the control of the kind Label that is called what we called the Label. Once we've found it we get the text from it, convert it to our number type (Int32, Int64, double, decimal etc) and add it to the totalling up number variable we've created.
foreach(RepeaterItem MyItem in MyGrid.Items)
{
Label InvoicePrice = MyItem.FindControl("InvoicePrice") as Label;
MyTotal = MyTotal + Convert.ToInt32(InvoicePrice.Text);
}
Lastly we just put the total where we want it, I've used a Literal...
PriceTotal.Text = MyTotal.ToString();
Reader's Comments
Name
Comment
Add a RELEVANT link (not required)
Upload an image (not required)
Uploading...
Home