Solved

Gridview Row Calculation

Posted on 2006-11-14
9
2,090 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now