通用代码
'--------------------------库存计算-----------------------------
Dim zd As New Dictionary(of String,String)
zd.Add("库存","物料编号")
zd.Add("本款本采购单","物料编号,规格,颜色,采购订单编号")
zd.Add("本规格库存","物料编号,规格,颜色")
Dim dr As DataRow
Dim mr As DataRow = e.DataRow
Dim drs As List(of DataRow)
For Each key As String In zd.Keys
Dim vs() As String = zd(key).Split(",")
Dim cs As New List(of String)
cs.AddRange(vs)
If cs.Contains(e.DataCol.Name) OrElse e.DataCol.Name = "入库" OrElse e.DataCol.Name = "出库" Then '找出上一行
dr = e.DataTable.Find("[_SortKey] < " & mr("_SortKey") & " And " & Functions.Execute("FilterStr_Buid",zd(key),mr,Nothing,Nothing), "[_SortKey] Desc")
If dr Is Nothing Then '如果没有找到上一行,说明本行就是第一行
mr(key) = mr("入库") - mr("出库")
dr = mr
End If
drs = e.DataTable.Select("[_SortKey] >= " & dr("_SortKey") & " And " & Functions.Execute("FilterStr_Buid",zd(key),dr,Nothing,Nothing), "[_SortKey]") '上一行以下的所有行-----------------
For i As Integer = 1 To drs.Count - 1 '重算余下行的余额
drs(i)(key) = drs(i-1)(key) + drs(i)("入库") - drs(i)("出库")
Next
If cs.Contains(e.DataCol.Name) AndAlso e.OldValue IsNot Nothing AndAlso e.OldValue <> e.NewValue Then
dr = e.DataTable.Find("[_SortKey] < " & mr("_SortKey") & " And " & Functions.Execute("FilterStr_Buid",zd(key),mr,e.DataCol.Name ,e.OldValue), "[_SortKey] Desc") '找出被修改行的上一个【修改列】的行------------------------
If dr Is Nothing Then
dr = e.DataTable.Find(Functions.Execute("FilterStr_Buid",zd(key),mr,e.DataCol.Name ,e.OldValue), "[_SortKey]") '被修改的就是第一行------------------------
If dr IsNot Nothing Then
dr(key) = dr("入库") - dr("出库")
End If
End If
If dr IsNot Nothing Then
drs = e.DataTable.Select("[_SortKey] >= " & dr("_SortKey") & " And " & Functions.Execute("FilterStr_Buid",zd(key),dr,e.DataCol.Name ,e.OldValue), "[_SortKey]") '重算余下所有行------------------------
For i As Integer = 1 To drs.Count - 1
drs(i)(key) = drs(i-1)(key) + drs(i)("入库") - drs(i)("出库")
Next
End If
End If
End If
Next