Solved

Gridview footer

Posted on 2008-10-24
7
615 Views
Last Modified: 2012-05-05
I am trying to display some runing totals in the footer of my gridview however I am a newb to asp.net. I am using a tutorial and this is the code for the method.

http://www.asp.net/learn/data-access/tutorial-15-vb.aspx

Please tell me what Northwind.ProductsRow is in the following code so I can translate in my code.


Dim _totalUnitPrice As Decimal = 0
Dim _totalNonNullUnitPriceCount As Integer = 0
Dim _totalUnitsInStock As Integer = 0
Dim _totalUnitsOnOrder As Integer = 0

Protected Sub ProductsInCategory_RowDataBound _
    (sender As Object, e As GridViewRowEventArgs) _
        Handles ProductsInCategory.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim product As Northwind.ProductsRow = _
            CType(CType(e.Row.DataItem, DataRowView).Row, Northwind.ProductsRow)

        If Not product.IsUnitPriceNull() Then
            _totalUnitPrice += product.UnitPrice
            _totalNonNullUnitPriceCount += 1
        End If

        If Not product.IsUnitsInStockNull() Then
            _totalUnitsInStock += product.UnitsInStock
        End If

        If Not product.IsUnitsOnOrderNull() Then
            _totalUnitsOnOrder += product.UnitsOnOrder
        End If
    ElseIf e.Row.RowType = DataControlRowType.Footer Then
        Dim avgUnitPrice As Decimal = _
            _totalUnitPrice / CType(_totalNonNullUnitPriceCount, Decimal)

        e.Row.Cells(1).Text = "Avg.: " & avgUnitPrice.ToString("c")
        e.Row.Cells(2).Text = "Total: " & _totalUnitsInStock.ToString()
        e.Row.Cells(3).Text = "Total: " & _totalUnitsOnOrder.ToString()
    End If
End Sub


0
Comment
Question by:soccerman777
  • 4
  • 2
7 Comments
 

Author Comment

by:soccerman777
Comment Utility
Here is my gridview. I just need to translate

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                DataSourceID="SqlDataSource9" CssClass="style1"  AlternatingRowStyle-CssClass="style2"    
                 
                 
                 
               
                style="top: 660px; left: 150px; position: absolute; height: 103px; width: 700px; bottom: 161px;"
                AllowPaging="True" DataKeyNames="pkey" ShowFooter="True" onrowdatabound="GridView1_RowDataBound"
               
               
                >
                <Columns>
                   
                    <asp:BoundField DataField="qty" HeaderText="QTY" SortExpression="qty" />
                    <asp:BoundField DataField="description" HeaderText="DESCRIPTION"
                        SortExpression="description" />
                    <asp:BoundField DataField="unitPrice" HeaderText="UNIT PRICE"
                        SortExpression="unitPrice" />
                    <asp:BoundField DataField="extendedAmount" HeaderText="EXTENDED AMOUNT"
                        SortExpression="extendedAmount" />
                    <asp:BoundField DataField="vehicleNumber" HeaderText="VEHICLE NUMBER"
                        SortExpression="vehicleNumber" />
                    <asp:CommandField HeaderText="DELETE ITEM" ShowDeleteButton="True" />

                     


                   
                   
                     

                </Columns>
                <AlternatingRowStyle CssClass="style2" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource9" runat="server"
                ConnectionString="<%$ Myconnection%>"
               
                SelectCommand="SELECT [sessionID], [description], [unitPrice], [extendedAmount], [vehicleNumber], [poNumber], [qty], [pkey] FROM [imtbl_templineitem] WHERE ([sessionID] = @sessionID)"
                DeleteCommand="DELETE FROM [imtbl_templineitem] WHERE [pkey] = @pkey"
                InsertCommand="INSERT INTO [imtbl_templineitem] ([sessionID], [description], [unitPrice], [extendedAmount], [vehicleNumber], [poNumber], [qty]) VALUES (@sessionID, @description, @unitPrice, @extendedAmount, @vehicleNumber, @poNumber, @qty)"
               
                UpdateCommand="UPDATE [imtbl_templineitem] SET [sessionID] = @sessionID, [description] = @description, [unitPrice] = @unitPrice, [extendedAmount] = @extendedAmount, [vehicleNumber] = @vehicleNumber, [poNumber] = @poNumber, [qty] = @qty WHERE [pkey] = @pkey">
                <SelectParameters>
                    <asp:ControlParameter ControlID="TextBox2" Name="sessionID" PropertyName="Text"
                        Type="String" />
                </SelectParameters>
                <DeleteParameters>
                    <asp:Parameter Name="pkey" Type="Decimal" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="sessionID" Type="String" />
                    <asp:Parameter Name="description" Type="String" />
                    <asp:Parameter Name="unitPrice" Type="Decimal" />
                    <asp:Parameter Name="extendedAmount" Type="Decimal" />
                    <asp:Parameter Name="vehicleNumber" Type="Decimal" />
                    <asp:Parameter Name="poNumber" Type="Decimal" />
                    <asp:Parameter Name="qty" Type="Decimal" />
                    <asp:Parameter Name="pkey" Type="Decimal" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="sessionID" Type="String" />
                    <asp:Parameter Name="description" Type="String" />
                    <asp:Parameter Name="unitPrice" Type="Decimal" />
                    <asp:Parameter Name="extendedAmount" Type="Decimal" />
                    <asp:Parameter Name="vehicleNumber" Type="Decimal" />
                    <asp:Parameter Name="poNumber" Type="Decimal" />
                    <asp:Parameter Name="qty" Type="Decimal" />
                </InsertParameters>
            </asp:SqlDataSource>
       
       
       
   
0
 
LVL 7

Expert Comment

by:aboredman
Comment Utility
You will find my anwser quite vague but i don't have much time to give you a more complete one right now.

Check out those video from microsoft, they're for winForms but i'm pretty sure you can figure it out

http://msdn.microsoft.com/en-us/vbasic/bb466226.aspx
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 500 total points
Comment Utility
>>Please tell me what Northwind.ProductsRow is in the following code so I can translate in my code.
The ProductsRow is part of a strongly typed dataset for the products table.
As such, the sample has to do the extra step of:
Dim product As Northwind.ProductsRow = _
CType(CType(e.Row.DataItem, DataRowView).Row, Northwind.ProductsRow)

to get to the data.

Since you are using a straight sqldatasource, try:


    Public SumQty As Integer = 0

    Public SumUnitPrice As Decimal = 0.0

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

        Select Case e.Row.RowType

            Case DataControlRowType.DataRow

                If IsNumeric(e.Row.DataItem("unitPrice")) = True Then

                    SumUnitPrice += CType(e.Row.DataItem("unitPrice"), Decimal)

                End If

                If IsNumeric(e.Row.DataItem("qty")) = True Then

                    SumQty += CType(e.Row.DataItem("qty"), Integer)

                End If

            Case DataControlRowType.Footer

                e.Row.Cells(0).Text = SumQty.ToString()

                e.Row.Cells(2).Text = FormatCurrency(SumUnitPrice, True, True)

        End Select

    End Sub

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Closing Comment

by:soccerman777
Comment Utility
Thanks. I have already done this using another method but atleast I understand it now.
0
 
LVL 37

Expert Comment

by:samtran0331
Comment Utility
>>I have already done this using another method

Out of curiosity...what's the other method?
0
 

Author Comment

by:soccerman777
Comment Utility
There are two fields that I need summary data for in the footer so I converted them to Template Fields
Then wrote a simple function that caculates the totals.

Her is  one of the template Field

<asp:TemplateField HeaderText="Qty" FooterStyle-Font-Bold="True">
                        <ItemTemplate>
                         <div align="right"> <%#Trim(GetQty(Integer.Parse(Eval("qty").ToString())))%></div>
                        </ItemTemplate>
                         <FooterTemplate>                      
                            <div align="right">Total Items: <%#GetTotalQty().ToString%></div>
                        </FooterTemplate>
                    </asp:TemplateField>

Here is the functions

Dim TotalQty As Integer = 0
    Function GetQty(ByVal Qty As Integer) As Integer
        TotalQty += Qty
        Return Qty
    End Function
    Function GetTotalQty() As Integer
        Return TotalQty
    End Function
0
 

Author Comment

by:soccerman777
Comment Utility
Buy the way the method I listed above is flawed if you use paging  is enabled in gridview.  For instance if I put in 11 items for $5 each. The first page will show the total $50 and the second will show $5. I have not looked yet to see if there is a simple fix besides turning pageing off.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now