Creating a variable from a datagrid total

Jackpanel

Freshman
Joined
Feb 23, 2004
Messages
35
I'm building an invoice page, and using a datagrid to display all the items in the order. Below the datagrid is a series of input boxes where the user enters tax, brokerage, duty, etc. I need to add these on the fly entered numbers to the calculated subtotal from the datagrid.

Is there a way to have the datagrid make this calculation and store it as a variable, or will I need to do a seperate database call to calculate the subtotal? I know the datagrid can be used to display a footer row with subtotals, but how would I use that value outside of the datagrid?

Code:
<asp:datagrid id="ItemGrid" runat="server" showheader="true"
 AutogenerateColumns="False" DataKeyField="ID">
    <Columns>
      <asp:BoundColumn DataField="PartNumber" runat="server" />
      <asp:BoundColumn DataField="Quantity" runat="server" />
      <asp:BoundColumn DataField="UnitSellPrice" runat="server" />
      <asp:BoundColumn DataField="ItemTotal" runat="server" />
    </Columns>
</asp:datagrid>

Example of what I'm looking for:

Part# | Quantity | Unit Price | Amount
Part# | Quantity | Unit Price | Amount
Part# | Quantity | Unit Price | Amount
Part# | Quantity | Unit Price | Amount
--------------------------------------

Subtotal |___________| <------- subtotal of the "Amount" field from datagrid
Tax |___________|
Brokerage |____________|
Duty |___________|
Total _____________ <---- Calculated on the fly
 
In case anyone is interested, I found a solution.

Code:
        dbconn = New OleDbConnection(ConfigurationSettings.AppSettings("connString"))
        sql = "SELECT ID, SortOrder, PartNumber, Description, UnitType, Quantity, UnitSellPrice FROM Orders WHERE Opportunity = '" & sOpportunity & "' ORDER BY SortOrder"
        DBAdapter = New OleDbDataAdapter(sql, dbconn)
        DBDataSet = New DataSet()
        DBAdapter.Fill(DBDataSet, "Orders")
        '-- Add  calculated column
        DBTable = DBDataSet.Tables("Orders")
        DBColumn = New DataColumn()
        DBColumn.ColumnName = "ItemTotal"
        DBColumn.DataType = System.Type.GetType("System.Decimal")
        DBColumn.Expression = "UnitSellPrice * Quantity"
        DBTable.Columns.Add(DBColumn)
        DBDataView = New DataView(DBDataSet.Tables("Orders"))
        ItemGrid.DataSource = DBDataView
        ItemGrid.DataBind()
        dbconn.Close()

        nSubTotal = DBTable.Compute("SUM(ItemTotal)", "")
        txtSubTotal.Text = format$(nSubTotal, "currency")
 
Back
Top