0

I was looking for a way to add a cumulative total column to my GridView in vb.net that would show a cumulative total of one of the numeric columns in the row. So basically:

Points | Running Total

2 | 2

1 | 3

-0.5 | 2.5

1.5 | 4

I saw some questions on cumulative totals using SQL Server and other databases, but I found nothing about strictly using GridView without changing any SQL, so I thought I would post my solution here.

1 Answers1

0

VB.net, something like that:

Dim val as Double = 0

For i as Integer = 0 to Me.grid.Rows.Count - 1
   val += Me.grid.Rows(i).Cells("Points").Value
   Me.grid.Rows(i).Cells("Running Total").Value = val
Next
okkko
  • 1,010
  • 1
  • 13
  • 22
  • You should have `Option Strict On`. Better to **On** the three options. – dr.null Aug 01 '21 at 16:33
  • @dr.null Wrote the code directly to SO, didn't check. What's missing? – okkko Aug 01 '21 at 16:56
  • Add `Option Strict On` as the very first line and check what the debugger will say about `val += Me.grid.Rows(i).Cells("Points").Value`. See [this](https://stackoverflow.com/questions/2454552/what-do-option-strict-and-option-explicit-do) for more. – dr.null Aug 01 '21 at 17:05
  • That code doesn't work.. the result didn't show in the column of datagridview – anisya salsyabila Aug 01 '21 at 18:57
  • @anisyasalsyabila I literally copy pasted the code in VS on a test form with a populated grid and... it does work with me. Read the error messages and find out specifically why it doesn't work. For example, maybe you named your columns differently... – okkko Aug 01 '21 at 20:44
  • OH MY GOD, IT'S REALLY WORK.. OHHHH THANK YOU VERY MUCH – anisya salsyabila Aug 03 '21 at 16:15