Solved

Gridview Row Calculation

Posted on 2006-11-14
9
2,086 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 6

Accepted Solution

by:
rhencullen earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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.
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

762 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

7 Experts available now in Live!

Get 1:1 Help Now