By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,908 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,908 IT Pros & Developers. It's quick & easy.

Running total for GridView.

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.