Gridview Row Calculation

Hi All

I have a Gridview which holds several columns containing Integer values. What I would like to be able to do is create another column called 'Total' which adds up the integer values within each row and shows the total within the end column.

Similar to my example below...

           Jan   Feb  Mar   Apr  Total
Rec1     2       5      8      5      20
Rec2     4       4      6      1      15

Is this possible? If so woudl any care to demo?

Many thanks all


Who is Participating?

Finally shove the following onto the above!

< / ItemTemplate>
< / asp:TemplateField>

And you should get a new total field containing the summation of the four month columns.

I use something very similar to swap a locale string for a URL to a flag image by testing the sucture string in a back end method and returning the appropriate URL string to the grid.

Lee SavidgeCommented:

I don't have the code on hand to do it, but the way you want to do it is to use the ItemDataBound event. This event will fire on each row returned to the GridView. From there you can process each column in a loop. You can access each column by using the column index as if you're processing an array.


In your code-behind class define a new method that will take the four parameters, sum them an return the result.

ie (Csharp)

protected int sumFourMonths (int m1, int m2, int m3, int m4)
return m1+m2+m3+m4;

Then in your gridview, assuming the 4 month fields are bound to your datasource, and are called month1 month2 month3 month4, add a new template field with an ItemTemplate

    <asp:label runat="server" id="lblTotal" Text='<%#
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Darned thing cutoff the second half of my post :-(  I'll try again

Append the following onto the post above:
< asp:TemplateField>
  < ItemTemplate>
<asp:label runat="server" id="lblTotal" Text='< % # sumFourMonths(Convert.ToInt32(DataBinder.Eval(Container.DataItem, "month1")),Convert.ToInt32(DataBinder.Eval(Container.DataItem, "month2")),Convert.ToInt32(DataBinder.Eval(Container.DataItem, "month3")),Convert.ToInt32(DataBinder.Eval(Container.DataItem, "month4"))) % >
Rats - it still missed the code to close off the item template and templatefield:  Maybe a line length issue

< asp:TemplateField>
  < ItemTemplate>
<asp:label runat="server" id="lblTotal" Text='< % # sumFourMonths(
        Convert.ToInt32(DataBinder.Eval(Container.DataItem, "month1")),
        Convert.ToInt32(DataBinder.Eval(Container.DataItem, "month2")),
        Convert.ToInt32(DataBinder.Eval(Container.DataItem, "month3")),
        Convert.ToInt32(DataBinder.Eval(Container.DataItem, "month4")))' % >
you can use gridview's rowdatabound event. in this event you basically access to current row. so you can access currentrow's cells.
please check for startup you start & try a bit on your own if stuck post code here we may find solution.

rito1Author Commented:
Thanks all for you help.

rhencullen, I went with your solution and it works well.

Kind Regards

Glad to have been able to help.

The comments about hooking the rowdatabound event are worth bearing in mind for the future.

I personally wouldn't hook the rowdatabound to achieve the result you were looking for, because to my mind it's easier to implement the method I suggested because you've already got all of the bindings visible and setup in the page without having to do anything more complex than the basic calculation in the back end - plus I find it easier to maintain when going back to the code later.

But if you want to do sum the contents of a column and list the result in the footer, or have the footer list the number of rows that contain a particular value then the RowDataBound event is invaluable.  (I've used this kind of thing in a CMS to list the number of rows in a GridView that have a particular boolean flag set to true).  The flags example I mentioned is also used in the same CMS on a different page to visually show which countries a product in the GridView can legally be sold to).

You've gotta love the flexibility of the GridView :-)

rito1Author Commented:
Thanks Paul,

I personally am just getting to grips with the power of the Gridview and how to use its events.

Once again, thanks for you help.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.