sqlvb.netgridviewcumulative-frequency

Cumulative frequency in datagridview by Visual Basic


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.


Solution

  • 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