Home > Back-end >  How to loop certain columns, and rows from DataGrid, sum the rows and display to a chart in vb.net
How to loop certain columns, and rows from DataGrid, sum the rows and display to a chart in vb.net

Time:11-24

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

CodePudding user response:

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

CodePudding user response:

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
  • Related