Solved

Gridview footer

Posted on 2008-10-24
7
620 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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