Visual Basic Development Bookmark and Share   
 Home > Visual Basic General > how to calculate sum in vb.net
 

how to calculate sum in vb.net

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
AgronSverkaj  Saturday, March 21, 2009 9:55 PM
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 byYichun Feng Friday, March 27, 2009 9:20 AM
  •  
Arjun Paudel  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:
DimiAsInteger
DimtotalAsDouble=0
Fori=0TodtProducts.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 byYichun Feng Friday, March 27, 2009 9:20 AM
  •  
Yichun Feng  Friday, March 27, 2009 3:26 AM
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 byYichun Feng Friday, March 27, 2009 9:20 AM
  •  
Arjun Paudel  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:
DimiAsInteger
DimtotalAsDouble=0
Fori=0TodtProducts.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 byYichun Feng Friday, March 27, 2009 9:20 AM
  •  
Yichun Feng  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)




AgronSverkaj  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.
Yichun Feng  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)

AgronSverkaj  Wednesday, April 08, 2009 8:51 PM

You can use google to search for other answers

Custom Search

More Threads

• How to read config file values for a library project?
• exporting proyects from VB.net 2003 + Crystal to VB.net 2005 + Crystal Reports
• Designing an interface related to VS2005
• vb 2008 time calculations
• can a vb.net product run an excel output in a pc without excel installed?
• Function problem
• Suppress warnings in Access 2003
• Custom Variable
• Display X-Y values in MSChart
• Image dimensions