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:
Dim dt As DataTable = DirectCast(DataGridView1.DataSource, DataTable)
Dim dt2 As DataTable = dt.Clone()
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()
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