Select Case e.DataCol.Name
Case "入库","出库","产品","产品ID"
Dim dr As DataRow
Dim mr As DataRow = e.DataRow
Dim drs As List(of DataRow)
Dim jk As Integer
Dim ck As Integer
dr = e.DataTable.Find("[_SortKey] < " & mr("_SortKey") & " And [产品] = '" & mr("产品") & "' and [产品ID] = '" & mr("产品ID") & "' ", "[_SortKey] Desc")
If dr Is Nothing Then '如果是第一行,计算结存
jk = DataTables("例子二").SQLCompute("Sum(入库)","[_SortKey] < " & mr("_SortKey") & " And [产品] = '" & mr("产品") & "' and [产品ID] = '" & mr("产品ID") & "' ")
ck = DataTables("例子二").SQLCompute("Sum(出库)","[_SortKey] < " & mr("_SortKey") & " And [产品] = '" & mr("产品") & "' and [产品ID] = '" & mr("产品ID") & "'")
mr("库存") = e.DataRow("入库") - e.DataRow("出库") + jk - ck
dr = mr
End If
drs = e.DataTable.Select("[_SortKey] >= " & dr("_SortKey") & " And [产品] = '" & dr("产品") & "' and [产品ID] = '" & mr("产品ID") & "'", "[_SortKey]")
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 "产品"
Dim dr As DataRow
Dim mr As DataRow = e.DataRow
Dim drs As List(of DataRow)
Dim jk As Integer
Dim ck As Integer
If e.OldValue IsNot Nothing Then
dr = e.DataTable.Find("[_SortKey] < " & mr("_SortKey") & " And [产品] = '" & e.OldValue & "' and [产品ID] = '" & mr("产品ID") & "' ", "[_SortKey] Desc") '找出旧产品的上一行
If dr Is Nothing Then '如果不存在上一行,那么本行就是旧产品的第一行,由于已经被修改为新产品,需要找出旧产品现在的第一行
dr = e.DataTable.Find("[产品] = '" & e.OldValue & "'and [产品ID] = '" & mr("产品ID") & "' ", "[_SortKey]") '找出旧产品现在的第一行
If dr IsNot Nothing Then '如果找到,计算库存
jk = DataTables("例子二").SQLCompute("Sum(入库)","[_SortKey] < " & mr("_SortKey") & " And 产品 = '" & e.OldValue & "' and [产品ID] = '" & mr("产品ID") & "' ")
ck = DataTables("例子二").SQLCompute("Sum(出库)","[_SortKey] < " & mr("_SortKey") & " And 产品 = '" & e.OldValue & "' and [产品ID] = '" & mr("产品ID") & "' ")
dr("库存") = dr("入库") - dr("出库") + jk - ck
End If
End If
If dr IsNot Nothing Then
drs = e.DataTable.Select("[_SortKey] >= " & dr("_SortKey") & " And [产品] = '" & dr("产品") & "' and [产品ID] = '" & mr("产品ID") & "' ", "[_SortKey]")
For i As Integer = 1 To drs.Count - 1
drs(i)("库存") = drs(i-1)("库存") + drs(i)("入库") - drs(i)("出库")
Next
End If
End If
Case "产品ID"
Dim dr As DataRow
Dim mr As DataRow = e.DataRow
Dim drs As List(of DataRow)
Dim jk As Integer
Dim ck As Integer
If e.OldValue IsNot Nothing Then
dr = e.DataTable.Find("[_SortKey] < " & mr("_SortKey") & " And [产品ID] = '" & e.OldValue & "' and [产品] = '" & mr("产品") & "' ", "[_SortKey] Desc") '找出旧产品的上一行
If dr Is Nothing Then '如果不存在上一行,那么本行就是旧产品的第一行,由于已经被修改为新产品,需要找出旧产品现在的第一行
dr = e.DataTable.Find("[产品ID] = '" & e.OldValue & "'and [产品] = '" & mr("产品") & "' ", "[_SortKey]") '找出旧产品现在的第一行
If dr IsNot Nothing Then '如果找到,计算库存
jk = DataTables("例子二").SQLCompute("Sum(入库)","[_SortKey] < " & mr("_SortKey") & " And 产品ID = '" & e.OldValue & "' and [产品] = '" & mr("产品") & "' ")
ck = DataTables("例子二").SQLCompute("Sum(出库)","[_SortKey] < " & mr("_SortKey") & " And 产品ID = '" & e.OldValue & "' and [产品] = '" & mr("产品") & "' ")
dr("库存") = dr("入库") - dr("出库") + jk - ck
End If
End If
If dr IsNot Nothing Then
drs = e.DataTable.Select("[_SortKey] >= " & dr("_SortKey") & " And [产品ID] = '" & dr("产品ID") & "' and [产品] = '" & mr("产品") & "' ", "[_SortKey]")
For i As Integer = 1 To drs.Count - 1
drs(i)("库存") = drs(i-1)("库存") + drs(i)("入库") - drs(i)("出库")
Next
End If
End If
End Select