Solved

Gridview footer

Posted on 2008-10-24
7
618 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
ID: 22795986
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
ID: 22796224
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
ID: 22798182
>>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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Closing Comment

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

Expert Comment

by:samtran0331
ID: 22798409
>>I have already done this using another method

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

Author Comment

by:soccerman777
ID: 22799333
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
ID: 22814500
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

12 Experts available now in Live!

Get 1:1 Help Now