473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Running total for GridView.

I am building a GridView that is displaying some money values in 3 columns.

I want to put the totals of each column in a label field (one for each
column) in the footer.

I was trying to figure out which was the faster way or more efficient way.

I originally thought about getting SQL to do it, but that would entail
either a second Select or some type of subquery or self join, which I am not
sure is the best way.

The other way was to just total the columns in the grid as I bind the grid
(or after in the PreRender event).

I usually like to do my cleaning up of the grid or assign colors to the text
in the grid (based on the data) in the PreRender event. I just loop through
the grid and do the changes at that point.

I could also do the changes during the ItemDataBind event but am not sure
which would be better - add them up in the ItemDataBind event with the
overhead of the event firing for each row or just do it all at once in the
PreRender event.

Even in the PreRender event - how would I get access to the label in the
Footer of the GridView to sum up the rows?

Thanks,

Tom
Dec 14 '07 #1
3 9802
Hi Tom,

Please refer to the code below to get the running sum of the column in the
footer for the GridView control.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim sum As Double = 0.0

Dim i As Integer
For i = 0 To Me.GridView1.Rows.Count - 1
Dim s As String = Me.GridView1.Rows(i).Cells(0).Text
Dim val As Double = Double.Parse(s,
System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.CurrentCulture)
sum = sum + val
Next

If e.Row.RowType = DataControlRowType.Footer Then
CType(e.Row.Cells(2).FindControl("Label1"), Label).Text = sum
End If
End Sub

Regards,
Manish

"tshad" wrote:
I am building a GridView that is displaying some money values in 3 columns.

I want to put the totals of each column in a label field (one for each
column) in the footer.

I was trying to figure out which was the faster way or more efficient way.

I originally thought about getting SQL to do it, but that would entail
either a second Select or some type of subquery or self join, which I am not
sure is the best way.

The other way was to just total the columns in the grid as I bind the grid
(or after in the PreRender event).

I usually like to do my cleaning up of the grid or assign colors to the text
in the grid (based on the data) in the PreRender event. I just loop through
the grid and do the changes at that point.

I could also do the changes during the ItemDataBind event but am not sure
which would be better - add them up in the ItemDataBind event with the
overhead of the event firing for each row or just do it all at once in the
PreRender event.

Even in the PreRender event - how would I get access to the label in the
Footer of the GridView to sum up the rows?

Thanks,

Tom
Dec 14 '07 #2
Howdy,

Just a small change, shouldn't be :

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.Footer then

Dim sum As Double = 0.0

Dim i As Integer
For i = 0 To Me.GridView1.Rows.Count - 1
Dim s As String = Me.GridView1.Rows(i).Cells(0).Text
Dim val As Double = Double.Parse(s,
System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.CurrentCulture)
sum = sum + val
Next

CType(e.Row.Cells(2).FindControl("Label1"), Label).Text = sum

End If
End Sub
Or even simpler

private sum as Double = 0.0

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

Dim row As GridViewRow = e.Row

if row.RowType = DataControlRowType.DataRow Then
Me.sum += Convert.ToDouble(CType(row.DataItem, DataRowView)("Double"))
else if row.RowType = DataControlRowType.Footer Then
CType(e.Row.Cells(2).FindControl("Label1"), Label).Text = Me.sum.ToString()
end if

End Sub

Hope this helps
--
Milosz
"Manish" wrote:
Hi Tom,

Please refer to the code below to get the running sum of the column in the
footer for the GridView control.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim sum As Double = 0.0

Dim i As Integer
For i = 0 To Me.GridView1.Rows.Count - 1
Dim s As String = Me.GridView1.Rows(i).Cells(0).Text
Dim val As Double = Double.Parse(s,
System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.CurrentCulture)
sum = sum + val
Next

If e.Row.RowType = DataControlRowType.Footer Then
CType(e.Row.Cells(2).FindControl("Label1"), Label).Text = sum
End If
End Sub

Regards,
Manish

"tshad" wrote:
I am building a GridView that is displaying some money values in 3 columns.

I want to put the totals of each column in a label field (one for each
column) in the footer.

I was trying to figure out which was the faster way or more efficient way.

I originally thought about getting SQL to do it, but that would entail
either a second Select or some type of subquery or self join, which I am not
sure is the best way.

The other way was to just total the columns in the grid as I bind the grid
(or after in the PreRender event).

I usually like to do my cleaning up of the grid or assign colors to the text
in the grid (based on the data) in the PreRender event. I just loop through
the grid and do the changes at that point.

I could also do the changes during the ItemDataBind event but am not sure
which would be better - add them up in the ItemDataBind event with the
overhead of the event firing for each row or just do it all at once in the
PreRender event.

Even in the PreRender event - how would I get access to the label in the
Footer of the GridView to sum up the rows?

Thanks,

Tom

Dec 14 '07 #3
"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.plwrote in message
news:8F**********************************@microsof t.com...
Howdy,

Just a small change, shouldn't be :

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.Footer then

Dim sum As Double = 0.0

Dim i As Integer
For i = 0 To Me.GridView1.Rows.Count - 1
Dim s As String = Me.GridView1.Rows(i).Cells(0).Text
Dim val As Double = Double.Parse(s,
System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.CurrentCulture)
sum = sum + val
Next

CType(e.Row.Cells(2).FindControl("Label1"), Label).Text = sum

End If
End Sub
Or even simpler

private sum as Double = 0.0

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

Dim row As GridViewRow = e.Row

if row.RowType = DataControlRowType.DataRow Then
Me.sum += Convert.ToDouble(CType(row.DataItem, DataRowView)("Double"))
else if row.RowType = DataControlRowType.Footer Then
CType(e.Row.Cells(2).FindControl("Label1"), Label).Text =
Me.sum.ToString()
end if

End Sub

Hope this helps
It did.

Thanks,

Tom.

--
Milosz
"Manish" wrote:
>Hi Tom,

Please refer to the code below to get the running sum of the column in
the
footer for the GridView control.

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
Dim sum As Double = 0.0

Dim i As Integer
For i = 0 To Me.GridView1.Rows.Count - 1
Dim s As String = Me.GridView1.Rows(i).Cells(0).Text
Dim val As Double = Double.Parse(s,
System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.CurrentCulture )
sum = sum + val
Next

If e.Row.RowType = DataControlRowType.Footer Then
CType(e.Row.Cells(2).FindControl("Label1"), Label).Text = sum
End If
End Sub

Regards,
Manish

"tshad" wrote:
I am building a GridView that is displaying some money values in 3
columns.

I want to put the totals of each column in a label field (one for each
column) in the footer.

I was trying to figure out which was the faster way or more efficient
way.

I originally thought about getting SQL to do it, but that would entail
either a second Select or some type of subquery or self join, which I
am not
sure is the best way.

The other way was to just total the columns in the grid as I bind the
grid
(or after in the PreRender event).

I usually like to do my cleaning up of the grid or assign colors to the
text
in the grid (based on the data) in the PreRender event. I just loop
through
the grid and do the changes at that point.

I could also do the changes during the ItemDataBind event but am not
sure
which would be better - add them up in the ItemDataBind event with the
overhead of the event firing for each row or just do it all at once in
the
PreRender event.

Even in the PreRender event - how would I get access to the label in
the
Footer of the GridView to sum up the rows?

Thanks,

Tom

Dec 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: fwells11 | last post by:
Hi there. As you will see from my questions, I am an SQL newb. I dabble but never get to spend enough time to get proficient so base any feeedback on that basis please. This is all theoretical...
4
by: Bill Dika | last post by:
Hi I am trying to calculate a running total of a calculated textbox (tbAtStandard) in GroupFooter1 for placement in a textbox (tbTotalAtStandard) on my report in Groupfooter0. The problem...
1
by: u473 | last post by:
I am scratching my head with the required quotes and parentheses. I started with an existing working Query with Running Total by date. Now I need to produce a running total by quarter day, so I...
16
by: Gandalf186 | last post by:
I need to create a query that produces running totals for every group within my table for example i wish to see: - Group A 1 5 9 15 Group B
2
by: Jana | last post by:
Using Access 97. Background: I have a main report called rptTrustHeader with a subreport rptTrustDetails in the Details section of the main report. The main report is grouped by MasterClientID. ...
12
jaccess
by: jaccess | last post by:
Hello all, I am trying to create a running total based on a specific date range that is to be entered into a form. I currently have the form set up with 2 text boxes (date1 and date2) which are...
6
by: Stuart Shay | last post by:
Hello All: I have a array which contains the totals for each month and from this array I want to get a running total for each month decimal month = new decimal; month = 254; (Jan) month =...
3
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a webform with several entry boxes and the user enters numbers in each box. I keep a running total (just adds all of the entries together) but am posting back to the server to do this. ...
6
by: fishercraigj | last post by:
How do I code variables for a simple "running total" box without using an array? IE: I have a "Points Earned" text box that the user inputs a value into. I have another output "Total Points...
2
by: Gelcys | last post by:
I feel as though I’m banging my head against a wall trying to get something done – Access is good at some parts and Excel at others. What I’m starting with is a txt file that I need to import weekly...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.