Solved

Gridview Row Calculation

Posted on 2006-11-14
9
2,097 Views
Last Modified: 2008-03-01
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

Rit

0
Comment
Question by:rito1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 17942308
Hi,

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.

Regards,

Lee
0
 
LVL 6

Expert Comment

by:rhencullen
ID: 17942977
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:TemplateField>
  <ItemTemplate>
    <asp:label runat="server" id="lblTotal" Text='<%#
0
 
LVL 6

Expert Comment

by:rhencullen
ID: 17943008
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"))) % >
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 6

Expert Comment

by:rhencullen
ID: 17943018
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")))' % >
0
 
LVL 6

Accepted Solution

by:
rhencullen earned 500 total points
ID: 17943024
AArghh!!!

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.

Paul
0
 
LVL 4

Expert Comment

by:satish_nagdev
ID: 17943489
hi,
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 http://forums.asp.net/thread/1426172.aspx. you start & try a bit on your own if stuck post code here we may find solution.

regards,
satish.
0
 
LVL 1

Author Comment

by:rito1
ID: 17947475
Thanks all for you help.

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

Kind Regards

Rit
0
 
LVL 6

Expert Comment

by:rhencullen
ID: 17949081
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 :-)

Paul
0
 
LVL 1

Author Comment

by:rito1
ID: 17950264
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.

Rit
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question