Solved

Gridview footer

Posted on 2008-10-24
7
626 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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