.NET Framework - Subtotals in Gridview

Asked By Hutt on 22-Jul-08 01:26 PM
I have searched the net but have been unable to find a solution to
subtotaling in a gridview. I have a gridview that is bounded to sqldatasource
where I would like to subtotal rows throughout the gridview based on column
value "Type". I am using VB.NET.  Thanks.
--
Hutty




Eliyahu Goldin replied on 23-Jul-08 09:10 AM
The way to do this sort of things is to use a repeater or a listview with
ItemTemplate made out of two html table rows. The first <tr> will be used as
a regular row. The second <tr> will contain subtotals. It will be hidden for
all items (rows) but the last item for each Type. You will need to handle
either ItemDataBound or PreRender event to calculate the subtotals, detect
the last item for every Type and show the subtotal <tr> for the detected
last items.

--
Eliyahu Goldin,
Software Developer
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
Hutt replied on 23-Jul-08 02:05 PM
I failed to mention I am using a nested gridview in application. The second
gridview shows the details of the master gridview where I want to display
totals.  My goal is to show hierarchial data with subtotals at major
intervals.  Will the repeater accomplish this task?  Thanks.
--
Hutty
rajiv gandhir replied to Hutt on 13-Apr-10 07:16 AM
I have searched the net but have been unable to find a solution to

subtotaling in a gridview. I have a gridview that is bounded to sqldatasource

where I would like to subtotal rows throughout the gridview based on column

value "Type".

I am using VB.NET.



Regards

RAJIV
Gregory A. Beamer replied to rajiv gandhir on 13-Apr-10 11:42 AM
You have two choices in ASP.NET (actually any .NET)

1. Filter the results prior to binding. This means calculating subtotals and
making them a new field before you bind
2. Intercepting the binding event and adding the subtotals.

Either will work. I prefer the first if it is every row. And you have two
choices on that one:

1. Refactor the query to add subtotal (if per row subtotal is the goal this
works well)
2. Refactor the data object to add subtotal

On the second point, you can add a column to a DataSet (if used) or have the
object with a subtotal field that is automatically calculated by the "domain
model" data object.

If none of this makes sense, you need to google the basics of domain
modeling or DataSets, and possibly the binding events in the Grid you are
using.

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
|    Think outside the box!                        |
************************************************