add a new row with totals - gridview

J

John

have a gridview where I need to add totals for each grouped data,

example
my grid looks like this
Sales 2006:
BMW 100
Lexus 75
Mercedes 70
Total Sales: 245

Sales 2005:
BMW 90
Lexus 40
Mercedes 10
Total Sales: 140

I have the formatting done for all but the Total Sales row, how can I add
that to my gridview?
 
B

Bruno Alexandre

it's easy :)

in one of the templates add the code

<footerItem>
<asp:label runat="server" id="lblTotal" text='<%= intTotal %>' />
</footerItem>

add
Dim intTotal as Integer = 0
at the begining of your script (outside Page_Load)

you probably use a function to calculate the totals of the year, just add
this line toyour code

intTotal += yourBrandTotal

If you are getting the total directly from the Database in a column named,
for example, tTotal you problable have the code like this in your template
item

<asp:label runat="server" id="lblBrandTotal" Text='<%# Eval("tTotal") %>' />


just it for this:
<asp:label runat="server" id="lblBrandTotal" Text='<%# addTotal(
Eval("tTotal") ) %>' />

and ad a function called addTotal like:

Function addTotal( myValue as integer ) as integer
intTotal += myValue
return myValue
End function

and that's it :)
 
J

John

I don't want the total in the footer. I want the total for each year like
this:
Sales 2006:
BMW 100
Lexus 75
Mercedes 70
Total Sales: 245
Sales 2005:
BMW 90
Lexus 40
Mercedes 10
Total Sales: 140

I will be adding a total in the footer of all the totals, but for this I
want the totals as above.
 
B

Bruno Alexandre

ok then,

using the gridview_rowcreate you can add a row with the totals everytime
you change the year

or

do that in the SQL Statment using GROUP BY Clause
 
J

John

do u have a code snippet of something like this? I can't use the SQL (stored
procedure) due to its used by other apps that don't want the data grouped or
rolled up as I need to do on my web for
 
B

Bruno Alexandre

ok...

(i don't have nothing, just the idea)

1. get the data from the SP to a SQLClient.DataReader
2. populate the GV with it
GridView.DataSource = dr
GridView.DataBind()
3. set a global variable to save the years (current row and previous row)
and the brand total
Public currentYear as string = ""
Public newYear as string = ""
Public totalForYear as integer = 0
4. in the GridView_DataBound you check if the year changed, imagine that you
present the year in a label
currentYear = ctype( gridview.findcontrol("myYearLabel", Label).Value
if oldYear <> currentYear then
the years are diferent, so Add a row here with the totals
else
oldYear = currentYear
totalForYear += cInt(ctype(gridview.findcontrol("myTotalBrandLabel",
Label).Value)
end if
5. to add a new row with the variable follow this article for better view of
the idea:
http://fredrik.nsquared2.com/viewpost.aspx?PostID=201


with the data on SQLClient.DataReader you can add row by row manually and
check when the year changes so you can add a new row with totals, must like
classic ASP 3.0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top