How to extract DataGridView another with sum-up cell values for same values for same column values in vb.net

Hello,

I have a DataGridView output like below;

Picture1.png
As you can some values are same with the red-boxed columns and they values on the right side.

I want to sum-up that values like below;

Picture2.png
What's the logic way to create a datagridview with like this structure which has source from another datagridview.
HakanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
I would use LINQ to sum up the values. The DataGrid itself can't do it. Have a look at https://stackoverflow.com/questions/13998258/datatable-group-by-with-linq-in-vb-net
HakanAuthor Commented:
Hi Mr. Moreau

Could you please help me how can i convert cell values to double?

I have no binding source in my datagridview first i turn it to datatable like in below link;
https://stackoverflow.com/questions/3109009/how-to-make-a-datatable-from-datagridview-without-any-datasource

After that i used your reference but the problem is i need to convert these cell values from string to double.

How can i do that ?

Thanks.
Éric MoreauSenior .Net ConsultantCommented:
you can try something like this:

dim str as string = "1e-06;
dim d as double = double.Parse(str)
Console.WriteLine(str)

Open in new window

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

HakanAuthor Commented:
I tried below and it gives me "Input string was not in a correct format."                  
  dRow(cell.ColumnIndex) = Double.Parse(cell.Value)

 Dim dgv As DataGridView = GUI.dgvCheckBodyComputed

            Dim dt As DataTable = New DataTable()
            For Each col As DataGridViewColumn In dgv.Columns
                dt.Columns.Add(col.HeaderText)
            Next
            For Each row As DataGridViewRow In dgv.Rows
                Dim dRow As DataRow = dt.NewRow
                For Each cell As DataGridViewCell In row.Cells
                    dRow(cell.ColumnIndex) = Double.Parse(cell.Value)
                Next
                dt.Rows.Add(dRow)
            Next

Open in new window

Éric MoreauSenior .Net ConsultantCommented:
on which value? because it should work! Hope you are not trying to do it on your Parent column!
HakanAuthor Commented:
I just checked and noticed i already add data to other columns like Mass, Cog-x, Cog-y, Cog-z in type of Double.

But it gives error for  .mass = MonthGroup.Sum(Function(r) r.Field(Of Double)("Mass")) as System.InvalidCastException: 'Specified cast is not valid.'

        Public Shared Sub TestMethod()
            Dim dgv As DataGridView = GUI.dgvCheckBodyComputed

            Dim dt As DataTable = New DataTable()
            For Each col As DataGridViewColumn In dgv.Columns
                dt.Columns.Add(col.HeaderText)
            Next
            For Each row As DataGridViewRow In dgv.Rows
                Dim dRow As DataRow = dt.NewRow
                For Each cell As DataGridViewCell In row.Cells
                    dRow(cell.ColumnIndex) = cell.Value
                Next
                dt.Rows.Add(dRow)
            Next

            Dim query = From row In dt
                        Group row By Parent = row.Field(Of String)("Parent") Into MonthGroup = Group
                        Select New With {
                            Key Parent,
                            .mass = MonthGroup.Sum(Function(r) r.Field(Of Double)("Mass")),
                            .cogx = MonthGroup.Sum(Function(r) r.Field(Of Double)("CoG-x")),
                            .cogy = MonthGroup.Sum(Function(r) r.Field(Of Double)("CoG-y")),
                            .cogz = MonthGroup.Sum(Function(r) r.Field(Of Double)("CoG-z"))
                       }
            For Each x In query

                GUI.DataGridView1.Rows.Add(x.mass, x.cogx, x.cogy, x.cogz)
            Next
        End Sub

Open in new window

Éric MoreauSenior .Net ConsultantCommented:
it works for me! You probably have incorrect values. Try with a smaller number of rows.

Public Class Form1

    Private mds As DataSet

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        CreateData()
        DataGridView1.DataSource = mds.Tables(0)

        Dim query = From row In mds.Tables(0)
                Group row By Parent = row.Field(Of String)("Parent") Into MonthGroup = Group
                Select New With {
                Key Parent,
                .mass = MonthGroup.Sum(Function(r) r.Field(Of Double)("Mass")),
                .cogx = MonthGroup.Sum(Function(r) r.Field(Of Double)("CoG-x"))
                }
        DataGridView2.DataSource = query.ToList()
    End Sub

    Private sub CreateData()
        Dim dt As New DataTable("Test")
        With dt
            .Columns.Add("Parent", GetType(String))
            .Columns.Add("Mass", GetType(Double))
            .Columns.Add("CoG-x", GetType(Double))

            'Add rows
            .LoadDataRow(New Object() {"Model", 1E-06,0}, True)
            .LoadDataRow(New Object() {"Model", 1E-06,0}, True)
            .LoadDataRow(New Object() {"Model", 1E-06,-0.5499}, True)
            .LoadDataRow(New Object() {"Model", 1E-06,-0.5499}, True)
            .LoadDataRow(New Object() {"Model", 1E-06,-0.5719}, True)
            .LoadDataRow(New Object() {"Model", 1E-06,-0.5719}, True)

            .LoadDataRow(New Object() {"@S_bush...", 5E-07,0.5561}, True)
            .LoadDataRow(New Object() {"@S_bush...", 5E-07,-0.839775}, True)
            .LoadDataRow(New Object() {"@S_bush...", 5E-07,0.5561}, True)
            .LoadDataRow(New Object() {"@S_bush...", 5E-07,0.5561}, True)

            .LoadDataRow(New Object() {"@S_fawheelend...", 35.61,-9.629649721}, True)
            .LoadDataRow(New Object() {"@S_fawheelend...", 35.61,-9.629649721}, True)
            .LoadDataRow(New Object() {"@S_fawheelend...", 55.3,-9.629649721}, True)
            .LoadDataRow(New Object() {"@S_fawheelend...", 55.3,-9.629649721}, True)
        End With

        mds = new DataSet()
        mds.Tables.Add(dt)
    End sub
End Class

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HakanAuthor Commented:
Thank you Mr. Moreau. You're right!. It's now corrected.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.