Start New Discussion within our Software Development Community

How can I get sum of total values from cells in DataGridView? I want it to look like this

Column1 Column2 Column3 Column4
String ---- String ---- String ---- 10
String ---- String ---- String ---- 7
String ---- String ---- String ---- 3
String ---- String ---- String ---- 5

I want to get number 25 (Sum from Column4).


Give this a try:
Remember that the column nubering is zero index based (Column4 = index 3)

    Dim sum As Int32 = (From dgvrow In DataGridView1.Rows _
                       Select CInt(CType(dgvrow, DataGridViewRow).Cells(3).Value)) _

Try this code and put it on a Button

Dim sum As Double = 0
For i = 0 To DatagridView1.RowCount - 1
sum += DatagridView1.Rows(i).Cells("ColumnName").Value() 'put your Column Name here.

TextBox1.text = sum 'the result will display in a TextBox

This works for me, even if there is text in one or more of the cells

On Error Resume Next
For i = 0 To DataGridView1.CurrentRow.Index
      sum = sum + DataGridView1.Rows(i).Cells("Column3").Value
Next i

the cells

This question has already been answered. Start a new discussion instead.