试试
Dim dic As new Dictionary(of String,Integer)
DataTables("出货信息_包装箱明细").DataRows.Clear()
For Each dr1 As DataRow In DataTables("出货信息_出货明细").DataRows
For Each dr2 As DataRow In DataTables("BOM基础数据").SQLSelect("[成品编码] = '" & dr1("成品编码") & "' and 材料类型 = '包装箱'")
Dim nm As String = dr1("销售订单号") & "|" & dr2("材料编码") & "|" & dr2("材料描述") & "|" & dr2("包装箱尺寸") & "|" & dr2("库位") & "|" & dr1("成品编码")
Dim vl As Integer = dr1("当日数量") * dr2("用量")
If dic.ContainsKey(nm) = False
dic.Add(nm,vl)
Else
dic(nm) = dic(nm)+ vl
End If
Next
Next
For Each key As String In dic.keys
Dim keys() As String = key.split("|")
Dim dr As DataRow = DataTables("出货信息_包装箱明细").AddNew()
Dim cr As DataRow = Tables("出货信息_出货明细").DataTable.Find("[成品编码] = '" & keys(5) & "'")
dr("材料编码") = keys(1)
dr("材料描述") = keys(2)
dr("包装箱尺寸") = keys(3)
dr("库位") = keys(4)
dr("销售订单号") = keys(0)
dr("需求数量") = dic(key)
dr("成品编码") = keys(5)
dr("成品描述") = cr("成品描述")
dr("下达日期") = cr("下单日期")
dr("业务员") = cr("业务员")
Next