HighTechTalks DotNet Forums  

Running total for GridView.

ASP.net ASP.net discussions (microsoft.public.dotnet.framework.aspnet)


Discuss Running total for GridView. in the ASP.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
tshad
 
Posts: n/a

Default Running total for GridView. - 12-14-2007 , 03:22 AM






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



Reply With Quote
  #2  
Old   
Manish
 
Posts: n/a

Default RE: Running total for GridView. - 12-14-2007 , 05:07 AM






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:

Quote:
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




Reply With Quote
  #3  
Old   
Milosz Skalecki [MCAD]
 
Posts: n/a

Default RE: Running total for GridView. - 12-14-2007 , 08:06 AM



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:

Quote:
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




Reply With Quote
  #4  
Old   
tshad
 
Posts: n/a

Default Re: Running total for GridView. - 12-18-2007 , 12:59 AM



"Milosz Skalecki [MCAD]" <mily242 (AT) DONTLIKESPAMwp (DOT) pl> wrote

Quote:
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.


Quote:
--
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






Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.