Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Adding a running total column to a gridviewHelpful Member! 

WalterHeisenberg (TechnicalUser)
24 Jun 10 11:45
Hello,

I have a simple gridview that has three columns: date, amount, and transaction type. I want to add a fourth column that keeps a running total of the amounts. The reason I need this and can't simply show a total in the footer is I will be allowing the users to remove rows from the gridview at runtime. I've been scouring the web and haven't really seen anything on this. I'm hoping someone could help me out. Thanks!
 
Helpful Member!  jmeckley (Programmer)
24 Jun 10 12:12

Quote:

The reason I need this and can't simply show a total in the footer is I will be allowing the users to remove rows from the gridview at runtime
I don't see how add/removing rows will effect your ability to calculate a value and place it in the footer.

if you want to stick with a running total by row make it part of the data object (another column in a datatable, or property on an object) calculate the total before binding to the grid.

Jason Meckley
Programmer
Specialty Bakers, Inc.

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

WalterHeisenberg (TechnicalUser)
24 Jun 10 12:41
Thanks for the quick reply Jason. After rereading my original post I can see it was worded quickly and poorly. For that I apologize.

This is a webpage for an Accounting Department and based on this running total they will be removing offsetting items from the gridview.

I am using an objectdatasource to fill up the gridview and it is this calculation of the running total itself I'm not so sure how to do. The only thing I can think of is adding another column that contains a label and in the rowdatabound event looping through to set the value. Is this the best way? Thought there might be a much simpler way I am not thinking of. Thanks for your time and help.
jmeckley (Programmer)
24 Jun 10 14:41
1st ditch the datasource controls. they do more harm than good. 2nd you can put the total in the footer like this

CODE

int total;

Page_Load(...)
{
   var data = GetDataFromDatabase();
   foreach(var row in data.Rows)
   {
      total+=row["amount"];
   }
   MyGrid.DataSource = data;
}

GridView_DataBound(...)
{
   MyGrid.Footer.Cells[n].Text = total.ToString("C");
}
you'll need to wire the handler above to the datagrid. I'll assume you can do that part. if not a quick google search on gridview events will answer that.

Jason Meckley
Programmer
Specialty Bakers, Inc.

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

WalterHeisenberg (TechnicalUser)
24 Jun 10 18:46
Thanks for your help Jason. I ended up getting the running total I needed by creating a RowDataBound event and sticking the following:

CODE

ColumnTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Amount"));
                e.Row.Cells[5].Text = ColumnTotal.ToString("c");

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close