Solved

Gridview Row Calculation

Posted on 2006-11-14
9
2,095 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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