0

I'm trying to loop certain columns and rows from a datagrid, sum those rows and show them on a chart.

I am able to do the row sums one by one with this code:

Dim sum = (From row As DataGridViewRow In dgvData.Rows.Cast(Of DataGridViewRow)()
                   Select CDec(row.Cells(5).Value)).Sum
X = sum.Tostring

then I write the column name manually in the chart

Me.Chart1.Series("Repairs").Points.AddXY("Unit Count", x)

and im able to do the row sums loop

For i As Integer = 5 To 15
            Dim sum = (From row As DataGridViewRow In dgvData.Rows.Cast(Of DataGridViewRow)()
                       Select CDec(row.Cells(i).Value)).Sum
            
                Me.Chart1.Series("Repairs").Points.AddXY("Unit Count", (sum.ToString))
            Next

but i dont know how to read the column headers

I don't know how to write a loop that would

  • look at columns headers 5-15 save their names (col)

  • loop those values into

    Me.Chart1.Series("Repairs").Points.AddXY("col", sumOFrows)

any help would be much appreciative

Cam385
  • 1
  • 1
  • 1
    Don't do the math on the DataGridView. How did you populate it? Do the math on that data source. – djv Nov 21 '21 at 03:39

2 Answers2

0

are you trying to sum the rows first, save the result in a column and then sum the "column result"? Sorry but i'm not 100% sure about the question

Lorenzo Martini
  • 373
  • 1
  • 8
  • I'm trying to loop read the column headers from row 5-15, I'm able to loop the sums of rows 5-15. – Cam385 Nov 22 '21 at 15:20
  • ok, check this: https://stackoverflow.com/questions/10179223/find-a-row-in-datagridview-based-on-column-and-value it should be helpful – Lorenzo Martini Nov 22 '21 at 15:25
0

got it to work like this:

Dim ids = New List(Of Integer)({5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26})
        For Each id In ids
            Dim sum = (From row As DataGridViewRow In dgvData.Rows.Cast(Of DataGridViewRow)()
                       Select CDec(row.Cells(id).Value)).Sum
           
            Dim fetchdgvheaders(dgvData.Columns.Count) As String
            fetchdgvheaders(id) = (dgvData.Columns(id).Name)


            Me.Chart1.Series("Repairs").Points.AddXY((fetchdgvheaders(id)), (sum.ToString))
           

        Next
Cam385
  • 1
  • 1