Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Running total for GridView.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tshad - 14 Dec 2007 08:22 GMT
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
Manish - 14 Dec 2007 10:07 GMT
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

> I am building a GridView that is displaying some money values in 3 columns.
>
[quoted text clipped - 25 lines]
>
> Tom
Milosz Skalecki [MCAD] - 14 Dec 2007 13:06 GMT
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
Signature

Milosz

> Hi Tom,
>
[quoted text clipped - 51 lines]
> >
> > Tom
tshad - 18 Dec 2007 05:59 GMT
> Howdy,
>
[quoted text clipped - 42 lines]
>
> Hope this helps

It did.

Thanks,

Tom.

>> Hi Tom,
>>
[quoted text clipped - 62 lines]
>> >
>> > Tom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.