下述代码系帮助中的流水账设计代码:
该代码在项目导入库存表后,直接在库存表中计算全对,没有任何问题!
将思路扩展到窗口设计中,利用SQL语句绑定表和绑定字段控件后,入库单窗体可以实现完美计算,但是出库单窗体就不能实现正确计算了,总是默认对应产品库存为零,然后计算所得永远是负数,不管入库多少都是负数!求纠正和指导!谢谢!
此主题相关图片如下:qq图片20150429233020.png
代码如下:
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("产品") & "'", "[_SortKey]")
For i As Integer = 1 To drs.Count - 1
drs(i)("库存") = drs(i-1)("库存") + drs(i)("入库") - drs(i)("出库")
Next
If e.DataCol.Name = "产品" AndAlso e.OldValue IsNot Nothing AndAlso e.OldValue <> e.NewValue Then
dr = e.DataTable.Find("[_SortKey] < " & mr("_SortKey") & " And [产品] = '" & e.OldValue & "'", "[_SortKey] Desc")
If dr Is Nothing Then
dr = e.DataTable.Find("[产品] = '" & e.OldValue & "'", "[_SortKey]")
If dr IsNot Nothing Then
dr("库存") = dr("入库") - dr("出库")
End If
End If
If dr IsNot Nothing Then
drs = e.DataTable.Select("[_SortKey] >= " & dr("_SortKey") & " And [产品] = '" & dr("产品") & "'", "[_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