Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Calculate Total Sum

  Asked By: Rena    Date: Nov 04    Category: Asp.net    Views: 2536

I'm trying to calculate the total price of all the articles in a dataGrid of a caddy and write it down in the footer...

The problem is that i have to do that dynamical and that my page is rebind all the time (add new row, dropdownlists...)

I've tried it this way but this just don't work... (found example on DataGridGirl...)

Some facts:

* I don't get any message by my debugger, so i guess there are no syntax-faults
* I don't see the string: "Totale prijs" into the footer
* Showfooter is set on true...

Somebody knows how to handle this?


Public Sub eventItemDataBound1(ByVal S As Object, ByVal e As DataGridItemEventArgs)
Dim drop1 As DropDownList = e.Item.FindControl("myDDL")
Dim indexInDataGrid As Integer = e.Item.ItemIndex

If Not IsNothing(drop1) Then 'zeer belangrijk => drop1 moet eerste bestaan!!!
' I Add dropdown here to the DataGrid...

'Bereken de Totale Som
Dim myTable As DataTable = myDataSet.Tables("OrderTabel")
Dim myDataRow As DataRow
Dim som As Double = 0

If (e.Item.ItemType = ListItemType.Footer) Then

For Each myDataRow In myTable.Rows

som = som + myTable.Rows(indexInDataGrid)("totaleprijs")

e.Item.Cells(1).Text = "Totale prijs:"
e.Item.Cells(7).Text = som
End If
End If
End Sub



3 Answers Found

Answer #1    Answered By: Jennie Harris     Answered On: Nov 04

Take a look at Scott Mitchell's An Extensive Exam of the DataGrid Web Control, Part 13 at aspnet.4guysfromrolla.com/articles/020503-1.aspx

Answer #2    Answered By: Melissa King     Answered On: Nov 04

i know all about that, txs, but i guess the problem  is that i bind every single column with <asp:bounddata...> to a datatable

In case of the footer this isn't the case, there i want do smth different and write  the value direct...
Is this possible?

Answer #3    Answered By: Clayton Richardson     Answered On: Nov 04

Your code  is totaly unoptimized. You get data for each item, and header and footer. You should put everything inside if statement and don't forget to cast types. when calculating price and displaying it in Text property of TableCell. It's strange that you don't get any casting errors.

Your variable indexInDataGrid is of the same value when calculating sum. You don't actually calculate  on each row  of the DataTable. "foreach" statement should be excanged with "for" statement with counter to calculate the sum.

for (int i = 0; i<myTable.ROws.Count; i++)
 sum  += (Double)myTable.Row[i]["totaleprijs"];

Didn't find what you were looking for? Find more on Calculate Total Sum Or get search suggestion and latest updates.