楼主的要求太特殊,弄起来挺麻烦......最好问一下狐爸如何动态增加列。
Dim tb As DataTable = DataTables("欠货明细表")
'获得汇总数据 Dim sums As New List(Of Integer) Dim data As New List(Of List(Of DataRow)) Dim ids As List(Of String) = tb.GetUniqueValues("", "产品型号") For Each id As String In ids Dim sum As Integer = tb.Compute("Sum(欠货数量)", "产品型号='" & id & "'") Dim drs As List(Of DataRow) = tb.Select("产品型号='" & id & "'", "合同编号") sums.Add(sum) data.Add(drs) Next
'得到列数 Dim max As Integer = 0 For Each d As List(Of DataRow) In data If max < d.Count Then max = d.Count End If Next
'生成临时表 Dim ntb As New DataTableBuilder("临时表") ntb.AddDef("产品型号", tb.baseTable.Columns("产品型号").dataType) ntb.AddDef("欠货数量", tb.baseTable.Columns("欠货数量").dataType) For i As Integer = 1 To max ntb.AddDef("欠货明细_合同编号" & i, tb.baseTable.Columns("合同编号").dataType) ntb.AddDef("欠货明细_欠货数量" & i, tb.baseTable.Columns("欠货数量").dataType) Next ntb.Build()
'填充数据 Dim t As Table = Tables("临时表") Dim count As Integer = 0 For Each d As List(Of DataRow) In data Dim r As Row = t.AddNew() Dim i As Integer = 0 For Each dr As DataRow In d If i = 0 Then r("产品型号") = dr("产品型号") r("欠货数量") = sums(count) End If i = i + 1 r("欠货明细_合同编号" & i) = dr("合同编号") r("欠货明细_欠货数量" & i) = dr("欠货数量") Next count = count + 1 Next
MainTable = t
|