TechArena Community Calculating sum of column in datagrid

#1
09-06-2009
 Member Join Date: Apr 2009 Posts: 79
Calculating sum of column in datagrid

I am looking for program which include a datagrid component on form. The control on the form will add the value like price, year, model no from given column and display total of the grid in the textbox which is at the top of the form.
#2
09-06-2009
 Member Join Date: May 2008 Posts: 2,008
Re: Calculating sum of column in datagrid

Code:
```  Dim sum As Decimal = 0
For Each row As DataGridViewRow In DataGridView1.Rows
row.Cells(37).Value = row.Cells(36).Value - row.Cells(6).Value * 113 / row.Cells(4).Value
sum += row.Cells(37).Value
Next
App.txtvalue.Text = sum```
#3
09-06-2009
 Member Join Date: May 2008 Posts: 2,293
Re: Calculating sum of column in datagrid

Code:
```Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles App.Load
Dim tb As New DataTable
Me.DataGridView1.DataSource = tb
End Sub
Private Sub DataGridView1_Columncalculated(ByVal sender As Object, ByVal i As System.Windows.Forms.DataGridViewcolumn) Handles DataGridView1.Columncalculated

Dim sum As BB = 0
For Each n As DataGridViewRow In Me.DataGridView1.Rows
sum = sum + r.Cells(0).Value - r.Cells(1).Value
r.Cells(2).Value = sum
Next
End Sub
End Class```
#4
09-06-2009
 Member Join Date: Jan 2009 Posts: 199
Re: Calculating sum of column in datagrid

Code:
```Dim CalSum As Double
Dim CalValue As Double
Dim DRow As Long

With MSFlexGrid1
' loop through all data rows
For DRow = .FixedRows To .Rows - 1
' get the value for the current row
CalValue = Val(.TextMatrix(lRow, 3))
' add it to our total
CalSum = CalSum + CalValue
Next DRow
.Rows = .Rows + 1
' Put the total in the new row
.TextMatrix(.Rows - 1, 3) = CalSum
End With```

 Tags: