Sum all values in datalist control
-
Good day friends! I taught this would be possible but I spend over 2hrs trying to figure this out but to no avail. This is what I want; I have datalist control that displays payment details and everything is working fine except one. Which is I want to display sum of all the payments made on a label control called Aggregate. For example Customer Name Amount Date Paul $2000 20-09-2016 Catherine $5000 20-09-2016 Otes $3000 20-09-2016 Aggregate = Sum of all payments made Any solution please... Thanks in advance...
-
Good day friends! I taught this would be possible but I spend over 2hrs trying to figure this out but to no avail. This is what I want; I have datalist control that displays payment details and everything is working fine except one. Which is I want to display sum of all the payments made on a label control called Aggregate. For example Customer Name Amount Date Paul $2000 20-09-2016 Catherine $5000 20-09-2016 Otes $3000 20-09-2016 Aggregate = Sum of all payments made Any solution please... Thanks in advance...
-
This sounds very easy to do. Where are you stuck?
There are only 10 types of people in the world, those who understand binary and those who don't.
Thanks for your reply. Am stuck at the summing. Help please
-
Thanks for your reply. Am stuck at the summing. Help please
Otekpo Emmanuel wrote:
Help please
I would love to but I have no idea what your problem is. It's like you went to the mechanic and said, "I can't drive my car." Do you think they could help?
There are only 10 types of people in the world, those who understand binary and those who don't.
-
Good day friends! I taught this would be possible but I spend over 2hrs trying to figure this out but to no avail. This is what I want; I have datalist control that displays payment details and everything is working fine except one. Which is I want to display sum of all the payments made on a label control called Aggregate. For example Customer Name Amount Date Paul $2000 20-09-2016 Catherine $5000 20-09-2016 Otes $3000 20-09-2016 Aggregate = Sum of all payments made Any solution please... Thanks in advance...
Handle ItemDataBound event to do the calculation. Here's a quick example for your reference:
int total = 0;
protected void DataList1_ItemDataBound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){
//access the Amount values here
//for example, if you are displaying it in a Label control
Label lbAmount = (Label) e.Item.FindControl("YourLabelControlID");
total += Convert.ToInt32(lbAmount.Text);
}
else if (e.Item.ItemType == ListItemType.Footer){
//Find the total label in the Footer
Label lblTotal = (Label)e.Item.FindControl("YourLabelControlID");
lblTotal.Text = total.ToString();
}
}Note: If your Amount value contains a dollar symbol, then you may need to trim that value first before doing a Convert.