I need to know how to add cells in a column in a datagridview

I have a datagrid view that I need to add the values in a column.  This code does not work.
Private Sub ReCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReCalc.Click
        Dim count As Integer = 0
        Dim DecimalVar As Decimal
        
        While count < WSW067DataGridView.RowCount
            DecimalVar += WSW067DataGridView.Rows(count).Item(10)
 
           count += 1
 
        End While
        lblCount.Text = count - 1
        lblTotal.Text = DecimalVar
    End Sub

Open in new window

glstackAsked:
Who is Participating?
 
Jorge PaulinoConnect With a Mentor IT Pro/DeveloperCommented:
Hi glstack,

You want to sum all the values in a column ?  Try this way:

        Dim total As Double = 0
        For Each row As DataGridViewRow In DataGridView2.Rows
            total += row.Cells("YourColumnName").Value
        Next

jpaulino
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Replace with WSW067DataGridView
0
 
glstackAuthor Commented:
Thanks!
0
All Courses

From novice to tech pro — start learning today.