?
Solved

Gridview footer

Posted on 2008-10-24
7
Medium Priority
?
634 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 2000 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
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!

 

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

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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