By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,326 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,326 IT Pros & Developers. It's quick & easy.

calculating total price from gridview values

P: 4
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:

Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
  2.                                         BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" 
  3.                                         CellPadding="4" CellSpacing="2" DataKeyNames="SessionID" 
  4.                                         DataSourceID="AccessDataSource1" OnRowDataBound="GridView2_RowDataBound"
  5.                                         EmptyDataText="There are currently no items in your basket." Font-Bold="True" 
  6.                                         Font-Names="Cambria" Font-Size="Medium" ForeColor="Black" ShowFooter="True" 
  7.                                         style="font-family: Cambria; font-size: medium">
  8.                                         <Columns>
  9.                                             <asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/x.jpg" 
  10.                                                 DeleteText="Remove" ShowDeleteButton="True" />
  11.                                             <asp:BoundField DataField="SessionID" HeaderText="SessionID" ReadOnly="True" 
  12.                                                 SortExpression="SessionID" Visible="False" />
  13.                                             <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
  14.                                                 SortExpression="ProductID" />
  15.                                             <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
  16.                                                 SortExpression="ProductName" />
  17.                                             <asp:BoundField DataField="ProductPrice" HeaderText="ProductPrice" 
  18.                                                 SortExpression="ProductPrice" />
  19.                                         </Columns>
  20.                                         <FooterStyle BackColor="#CCCCCC" />
  21.                                         <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
  22.                                         <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
  23.                                         <RowStyle BackColor="White" />
  24.                                         <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
  25.                                         <SortedAscendingCellStyle BackColor="#F1F1F1" />
  26.                                         <SortedAscendingHeaderStyle BackColor="Gray" />
  27.                                         <SortedDescendingCellStyle BackColor="#CAC9C9" />
  28.                                         <SortedDescendingHeaderStyle BackColor="#383838" />
  29.                                     </asp:GridView>
  30.                                     <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
  31.                                         DataFile="~/Db1.accdb" 
  32.                                         DeleteCommand="DELETE * FROM [Basket] WHERE [SessionID] = ?" 
  33.                                         SelectCommand="SELECT * FROM [Basket]">
  34.                                         <DeleteParameters>
  35.                                             <asp:Parameter Name="SessionID" Type="Int32" />
  36.                                         </DeleteParameters>
  37.                                     </asp:AccessDataSource>
... And here is the code for the rowdatabound for the gridview:

Expand|Select|Wrap|Line Numbers
  1. Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
  2.  
  3.  
  4.         Dim priceTotal As Decimal = 0
  5.  
  6.  
  7.  
  8.         If e.Row.RowType = DataControlRowType.DataRow Then
  9.             ' add the UnitPrice and QuantityTotal to the running total variables
  10.             priceTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ProductPrice"))
  11.  
  12.         ElseIf e.Row.RowType = DataControlRowType.Footer Then
  13.             e.Row.Cells(3).Text = "Totals:"
  14.  
  15.             e.Row.Cells(4).Text = priceTotal.ToString("c")
  16.  
  17.  
  18.             e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Right
  19.             e.Row.Cells(2).HorizontalAlign = HorizontalAlign.Right
  20.             e.Row.Font.Bold = True
  21.         End If
  22.  
  23.     End Sub
  24.  
Any help would be greatly appreciated. Thank you!
Apr 4 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.