Click here to Skip to main content
14,385,566 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All, could you help, summary the data in DGview1 to DGview2. here's my table.

Name	Hours
Name1	1
Name1	1
Name1	2
Name1	3
Name1	1
Name2	3
Name2	3
Name2	3


Expected output:
Name	Hours
Name1	8
Name2	9


What I have tried:

I used, for loop on the first but not the expected output.

For util As Integer = 0 To DataGridView1.RowCount - 2
        If DataGridView1.Rows(util).Cells(0).Value = "Name1" Then
        Dim totalUtil As Double
        totalUtil += Convert.ToDecimal(DataGridView1.Rows(util).Cells(5).Value)
        DataGridView4.Rows.Add(DataGridView1.Rows(util).Cells(0).Value, totalUtil)
        End If
        Next


Output:
Name Hours
Name1 1
Name1 2
Name1 5
Name1 11
Name1 so on..
Name2 so on..
Posted
Updated 7-Nov-19 3:16am
v3
Rate this:
Please Sign up or sign in to vote.

Solution 2

Using Linq[^]...

Assuming that your DataGridView1 control is bind with DataTable object, you can create another datatable object and fill-in it with grouped data. For example:

'for test purposes only
'Dim dt As DataTable = New DataTable()
'dt.Columns.Add(New DataColumn("Name", Type.GetType("System.String")))
'dt.Columns.Add(New DataColumn("Hours", Type.GetType("System.Int32")))
'dt.Rows.Add(New Object(){"Name1", 1})
'dt.Rows.Add(New Object(){"Name1", 1})
'dt.Rows.Add(New Object(){"Name1", 2})
'dt.Rows.Add(New Object(){"Name1", 3})
'dt.Rows.Add(New Object(){"Name1", 1})
'dt.Rows.Add(New Object(){"Name2", 3})
'dt.Rows.Add(New Object(){"Name2", 3})
'dt.Rows.Add(New Object(){"Name2", 3})

'get datatable object
Dim dt As DataTable = DirectCast(DataGridView1.DataSource, DataTable)
'create a datasource for DataGridView2
Dim dt2 As DataTable = dt.Clone()
'fill-in a datatable object
dt2 = dt.AsEnumerable() _
	.GroupBy(Function(x) x.Field(Of String)("Name")) _
	.Select(Function(grp) dt2.LoadDataRow( New Object(){ _
			grp.Key, _
			grp.Sum(Function(x) x.Field(Of Integer)("Hours")) _
		}, False)) _
	.CopyToDataTable()
'bind data
DataGridView2.DataSource = dt2


In case, you've got unbounded DataGridView control, you can operate on DataGridView's rows:
Dim result = dgv.Rows.Cast(Of DataGridViewRow)() _
	.GroupBy(Function(x) x.Cells(0).Value) _
	.Select(Function(grp) New With _
		{
			.Name = grp.Key, _
			.Hours = grp.Sum(Function(x) x.Cells(1).Value) _
		}) _
	.ToList()
DataGridView2.DataSource = result
   
Rate this:
Please Sign up or sign in to vote.

Solution 1

Your logic is somewhat confused, as you create a new total variable each time the entry is for "Name1", and add that number (the original) to the other grid. It should be something like:
Dim totalUtil As Double
' this loop calculates the totals of all Name1 entries
For util As Integer = 0 To DataGridView1.RowCount - 2
    If DataGridView1.Rows(util).Cells(0).Value = "Name1" Then
        totalUtil += Convert.ToDecimal(DataGridView1.Rows(util).Cells(5).Value)
    End If
Next
' store the total value in the other DataGridView
DataGridView4.Rows.Add(DataGridView1.Rows(util).Cells(0).Value, totalUtil)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100