This Sample Demonstrates how to Calculate Sum of Values From a DataGridView column.
You can use this code in many scenarios.
Like Calculating Total Products From a Particular Category etc.
In This code we will calculate points of an individual Candidate.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tot As Integer = 0
For i As Integer = 0 To DataGridView1.RowCount - 1
If DataGridView1.Rows(i).Cells(1).Value = txtName.Text Then
tot += DataGridView1.Rows(i).Cells(2).Value
End If
Next
If tot = 0 Then
MessageBox.Show("No Records Found")
End If
txtTotal.Text = tot
End Sub
In the Sample Code I have used a Dataset you can use the same code if you are working with a database.
Note * : Do Not Forget to change the index of the columns you want to calculate.
Regards Hefin Dsouza