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).

Thanks.

Recommended Answers

All 4 Replies

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)) _
                       .Sum()

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.
Next

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

Thanks!

All answers are correct! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.