Select Case e.DataCol.Name
Case "退库","进库","出库"
If e.DataRow("品名")<>"" And e.DataRow("品牌")<>"" And e.DataRow("规格")<>"" Then
Dim dr As DataRow
Dim mr As DataRow = e.DataRow
Dim drs As List(of DataRow)
Dim lastc As Integer
dr = e.DataTable.Find("[_SortKey] < " & mr("_SortKey") & " And [品名] = '" & mr("品名") & "'" & " And [品牌] = '" & mr("品牌") & "'" & " And [规格] = '" & mr("规格") & "'","[_SortKey] Desc")
If dr Is Nothing Then
mr("库存") = mr("退库") + mr("进库") - mr("出库")
lastc = mr("库存")
dr = mr
End If
drs = e.DataTable.Select("[_SortKey] >= " & dr("_SortKey") & " And [品名] = '" & mr("品名") & "'" & " And [品牌] = '" & mr("品牌") & "'" & " And [规格] = '" & mr("规格") & "'","[_SortKey]")
For i As Integer = 1 To drs.Count - 1
drs(i)("库存") = drs(i-1)("库存") + drs(i)("退库") + drs(i)("进库") - drs(i)("出库")
lastc = drs(i)("库存")
Next
Dim dc As DataTable = DataTables("库存表")
Dim mc As DataRow
Dim lc As Boolean = False
If dc.dataRows.count=0
mc = dc.addnew()
mc("所属")="重庆中烟工业公司"
mc("品名")=mr("品名")
mc("品牌") =mr("品牌")
mc("规格")=mr("规格")
mc("单位")=mr("单位")
mc("库存")=lastc
Else
For Each mc In dc.Select("")
If mc("所属")="重庆中烟工业公司" And mc("品名")=mr("品名") And mc("品牌") =mr("品牌") And mc("规格")=mr("规格") Then
mc("库存")=lastc
lc = True
Exit For
End If
Next
If not lc Then
mc = dc.addnew()
mc("所属")="重庆中烟工业公司"
mc("品名")=mr("品名")
mc("品牌") =mr("品牌")
mc("规格")=mr("规格")
mc("单位")=mr("单位")
mc("库存")=lastc
End If
End If
Else
messagebox.show("请先选择品名品牌和规格")
End If
End Select
因为便于好看和操作,对不同单位建立了一个物资流水账,然后对每种物资建立了一个最终库存表。在每个单位的流水账出库进库操作后,利用datacolchanged事件自动在最终库存表的库存数反映出来。这样每次进库出库都会发生datacolchanged事件。删除一条流水账记录也会发生datacolchanged事件。就是不知道出库进库清零是两个动作,会引发一次还是两次datacolchanged事件?