none
how to calculate sum in vb.net

    Question

  • Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            dtProducts = New DataTable("Products")
            dtProducts.Columns.Add("ID", GetType(Int32))
            dtProducts.Columns.Add("Name", GetType(String))
            dtProducts.Columns.Add("Quality", GetType(Double))
            dtProducts.Columns.Add("Price", GetType(Double))
            dtProducts.Columns.Add("Sum", GetType(Double))

    '---- this columns i need to calculate quality * Price= sum

            With dtProducts.Columns("ID")

                .AutoIncrement = True
                .AutoIncrementSeed = 1
            End With

            dsNorthwind = New DataSet("Northwind")
            dsNorthwind.Tables.Add(dtProducts)
            dgProducts.DataSource = dsNorthwind
            dgProducts.DataMember = "Products"

        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            dsNorthwind.WriteXml(xmlFile)

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If File.Exists(xmlFile) Then
                dsNorthwind.ReadXml(xmlFile)

            End If

        End Sub
    End Class
    Saturday, March 21, 2009 9:55 PM

Answers

  • Just put the expression

    ...................................................
    dtProducts.Columns.Add("Quality", GetType(Double))
     dtProducts.Columns.Add("Price", GetType(Double))

     dtProducts.Columns.Add("Sum", GetType(Double), "Quality*Price")
    .........................................................

    Arjun Paudel
    • Marked as answer by Yichun Feng Friday, March 27, 2009 9:20 AM
    Sunday, March 22, 2009 4:09 AM
  • Hi AgronSverkaj,

    If you want to calculate the sum of certain column, you can write code like this:
            Dim i As Integer 
            Dim total As Double = 0  
            For i = 0 To dtProducts.Rows.Count - 1  
                total = total + dtProducts(i)("Sum")  
            Next 
            MsgBox(total) 

    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Yichun Feng Friday, March 27, 2009 9:20 AM
    Friday, March 27, 2009 3:26 AM

All replies

  • Just put the expression

    ...................................................
    dtProducts.Columns.Add("Quality", GetType(Double))
     dtProducts.Columns.Add("Price", GetType(Double))

     dtProducts.Columns.Add("Sum", GetType(Double), "Quality*Price")
    .........................................................

    Arjun Paudel
    • Marked as answer by Yichun Feng Friday, March 27, 2009 9:20 AM
    Sunday, March 22, 2009 4:09 AM
  • Hi AgronSverkaj,

    If you want to calculate the sum of certain column, you can write code like this:
            Dim i As Integer 
            Dim total As Double = 0  
            For i = 0 To dtProducts.Rows.Count - 1  
                total = total + dtProducts(i)("Sum")  
            Next 
            MsgBox(total) 

    Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Yichun Feng Friday, March 27, 2009 9:20 AM
    Friday, March 27, 2009 3:26 AM
  • TextBox1.TextChanged 
    
    Dim i As Integer
    
    Dim total As Double = 0 
    
    total = total + dtProducts(i)("Sum")             
    
          'The problem is Here 
    
    MsgBox(total)
    
    
    
    
    Friday, March 27, 2009 10:19 PM
  • Hi AgronSverkaj,

    Try this:
    total = total + (CType(dt(i)("sum"), Double))

    And if it doesn't work, please give the description of error message. So that we can better idea of you problem.

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, March 30, 2009 1:11 AM
  •  

    Dim i As Integer

     

    Dim total As Double = 0

     

    For i = 0 To dtProducts.Rows.Count - 1

    total = total + (

    CType(dt(i)("sum"), Double))

    'By dt is error the msg is name dt is not declared this is dhe message of errore


    Next

    MsgBox(total)

    Wednesday, April 08, 2009 8:51 PM