![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
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 |
#4
| |||
| |||
|
|
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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |