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

Adding a running total column to a gridview 1

Status
Not open for further replies.

WalterHeisenberg

Technical User
Mar 28, 2008
159
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!
 
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
faq732-7259
 
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.
 
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
faq732-7259
 
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");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top