calculating total price from gridview values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AndrewStone
    New Member
    • Mar 2013
    • 4

    calculating total price from gridview values

    Good afternoon... I have searched for a solution, but yet to have success... I basically have a fictional shopping cart that contains a column for price. In the footer of the gridview, i need to show the total price of the 'ProductPrice' column. I need the total price to update everytime records are deleted... Is this possible? Currently my total price is stuck at '0' and does not change.

    Here is the code for the gridview:

    Code:
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
                                            BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" 
                                            CellPadding="4" CellSpacing="2" DataKeyNames="SessionID" 
                                            DataSourceID="AccessDataSource1" OnRowDataBound="GridView2_RowDataBound"
                                            EmptyDataText="There are currently no items in your basket." Font-Bold="True" 
                                            Font-Names="Cambria" Font-Size="Medium" ForeColor="Black" ShowFooter="True" 
                                            style="font-family: Cambria; font-size: medium">
                                            <Columns>
                                                <asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/x.jpg" 
                                                    DeleteText="Remove" ShowDeleteButton="True" />
                                                <asp:BoundField DataField="SessionID" HeaderText="SessionID" ReadOnly="True" 
                                                    SortExpression="SessionID" Visible="False" />
                                                <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                                                    SortExpression="ProductID" />
                                                <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                                                    SortExpression="ProductName" />
                                                <asp:BoundField DataField="ProductPrice" HeaderText="ProductPrice" 
                                                    SortExpression="ProductPrice" />
                                            </Columns>
                                            <FooterStyle BackColor="#CCCCCC" />
                                            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                                            <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                                            <RowStyle BackColor="White" />
                                            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                                            <SortedAscendingCellStyle BackColor="#F1F1F1" />
                                            <SortedAscendingHeaderStyle BackColor="Gray" />
                                            <SortedDescendingCellStyle BackColor="#CAC9C9" />
                                            <SortedDescendingHeaderStyle BackColor="#383838" />
                                        </asp:GridView>
                                        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
                                            DataFile="~/Db1.accdb" 
                                            DeleteCommand="DELETE * FROM [Basket] WHERE [SessionID] = ?" 
                                            SelectCommand="SELECT * FROM [Basket]">
                                            <DeleteParameters>
                                                <asp:Parameter Name="SessionID" Type="Int32" />
                                            </DeleteParameters>
                                        </asp:AccessDataSource>
    ... And here is the code for the rowdatabound for the gridview:

    Code:
    Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
    
    
            Dim priceTotal As Decimal = 0
    
    
    
            If e.Row.RowType = DataControlRowType.DataRow Then
                ' add the UnitPrice and QuantityTotal to the running total variables
                priceTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ProductPrice"))
    
            ElseIf e.Row.RowType = DataControlRowType.Footer Then
                e.Row.Cells(3).Text = "Totals:"
    
                e.Row.Cells(4).Text = priceTotal.ToString("c")
    
    
                e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Right
                e.Row.Cells(2).HorizontalAlign = HorizontalAlign.Right
                e.Row.Font.Bold = True
            End If
    
        End Sub
    Any help would be greatly appreciated. Thank you!
Working...