已实现,测试成功,本想附上程序运行后截图,但不知怎么发,只好发代码了!希望用完立即给分,本人急需。谢谢!
假设你原来的数据是存在名为:DataGridView1的控件里且已经有数据,那么转换后的数据会显示在DataGridView2控件里(DataGridView2控件不要进行任何设置,拖入窗口进行,切记!!!),Button1按钮按下后即可完成转换,具体实现如下(Copy后即可运行):
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable
For Each col As DataGridViewColumn In DataGridView1.Columns
dt.Columns.Add(col.HeaderText, System.Type.GetType("System.String"))
Next
Dim tmpList As New List(Of String)
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).Value Is Nothing Then Exit For
If tmpList.Contains(row.Cells(0).Value) = False Then tmpList.Add(row.Cells(0).Value)
Next
For Each s As String In tmpList
Dim dr As DataRow
dr = dt.NewRow
dr.Item(0) = s
dr.Item(1) = 0
dr.Item(2) = 0
dr.Item(3) = 0
dt.Rows.Add(dr)
Next
For i As Integer = 0 To dt.Rows.Count - 1
For Each row As DataGridViewRow In DataGridView1.Rows
If dt.Rows(i).Item(0) = row.Cells(0).Value Then
dt.Rows(i).Item(1) = row.Cells(1).Value
dt.Rows(i).Item(2) = Val(row.Cells(2).Value) + Val(dt.Rows(i).Item(2))
dt.Rows(i).Item(3) = Val(row.Cells(3).Value) + Val(dt.Rows(i).Item(3))
End If
Next
Next