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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sum values and keep running total with repeater

Status
Not open for further replies.

mcnewsw8

Programmer
Feb 26, 2013
3
0
0
i need to to add debits and credits columns and display the sum in a balance column that will be a running total. the debits and credits values come from the databaes. balance is calculated on the fly.

Income Expense Balance
100 0 100
50 25 125
500 0 625
0 30 595
etc

i am using the code below to attempt to make this work. it looks good on the first page, but the counter gets off on the 2nd and 3rd page. when i click the back page it goofs things up even more. my variable x is just a class variable so i think i need a better way to deal with the running balance.

protected void dataAccting_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//int x = 0;
RepeaterItem item = e.Item;
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView row = e.Item.DataItem as DataRowView;
Literal income = (Literal)item.FindControl("lit_Income");
Literal expense = (Literal)item.FindControl("lit_Expense");
if (income.Text == "")
{ income.Text = "0"; }
if (expense.Text == "")
{ expense.Text = "0"; }
income.Text = income.Text.Replace("$", "");
expense.Text = expense.Text.Replace("$", "");
//decimal expense = Convert.ToDecimal(row["Expense"].ToString());
Label t = (Label)e.Item.FindControl("lblBalance");
x += Convert.ToDecimal(income.Text) - Convert.ToDecimal(expense.Text);
t.Text = x.ToString();
}
}


thanks for any help.




 
There are many examples of how to do this online.
Just use the ItemDataBound event as you are.
Check the item type as you are. If a Item or AlternateItem, get the values you want, and keep a running total in a var.
Then, if it is the footer, use FindControl() to find a label(for example) and display the total from your var in there.
 
i guess my example isn't clear or you didn't look at it closely enough. my running total [Balance] will be displayed as the rows are bound - not at the end of the repeater in the footer. and you can see that is what i am trying to do in databound method.

Income Expense Balance
100 0 100
50 25 125
500 0 625
0 30 595
 
I guess I didn't. I thought you wanted a total in a footer. I see now you want a running total.
So what exactly is the problem? Is it when you are paging? So you go to the second page, for example, and the totals are off?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top