晕倒,原来你是用流水账,这样吧,来两段:
Select Case e.DataCol.Name
Case "采购单位","入库金额","付款金额"
Dim dr As DataRow
Dim mr As DataRow = e.DataRow
Dim drs As List(of DataRow)
dr = e.DataTable.Find("[_SortKey] < " & mr("_SortKey") & " And [采购单位] = '" & mr("采购单位") & "'", "[_SortKey] Desc")
If dr Is Nothing Then '如果没有上一行,说明本行就是同产品的第一行
mr("实际余额") = mr("入库金额") - mr("付款金额")
dr = mr
End If
drs = e.DataTable.Select("[_SortKey] >= " & dr("_SortKey") & " And [采购单位] = '" & dr("采购单位") & "'")
For i As Integer = 1 To drs.Count - 1 '重算余下行的余额
drs(i)("实际余额") = drs(i-1)("实际余额") + drs(i)("入库金额") - drs(i)("付款金额")
Next
End Select
Select Case e.DataCol.Name
Case "采购单位","入库金额","开piao金额"
Dim dr As DataRow
Dim mr As DataRow = e.DataRow
Dim drs As List(of DataRow)
dr = e.DataTable.Find("[_SortKey] < " & mr("_SortKey") & " And [采购单位] = '" & mr("采购单位") & "'", "[_SortKey] Desc")
If dr Is Nothing Then '如果没有上一行,说明本行就是同产品的第一行
mr("欠piao金额") = mr("入库金额") - mr("开piao金额")
dr = mr
End If
drs = e.DataTable.Select("[_SortKey] >= " & dr("_SortKey") & " And [采购单位] = '" & dr("采购单位") & "'")
For i As Integer = 1 To drs.Count - 1 '重算余下行的余额
drs(i)("欠piao金额") = drs(i-1)("欠piao金额") + drs(i)("入库金额") - drs(i)("开piao金额")
Next
End Select