流水账与重置列下面都是重置
问题一:哪个更高效?
问题二:"库存表"增加一列为”期初数“,要加在下面代码哪个位置?
方法一
Dim drs As List(of DataRow) = DataTables("库存表").Select("", "产品,日期")
Dim prev As String = ""
For i As Integer = 0 To drs.count -1
Dim cur As String = drs(i)("产品")
If cur = prev Then
drs(i)("库存") = drs(i - 1)("库存") + drs(i)("入库") - drs(i)("出库")
Else
drs(i)("库存") = drs(i)("入库") - drs(i)("出库")
prev = cur
End If
Next
方法二
Dim nms As New List(Of String)
Dim drs As New List(of DataRow)
With DataTables("表名")
nms = .GetValues("产品")
For Each nm As String In nms '找出每个产品的第一行数据, 添加到集合drs中
drs.Add(.Find("产品 = '" & nm & "'", "[_SortKey]"))
Next
For Each dr As DataRow In drs
.DataCols("入库").RaiseDataColChanged(dr)
Next
End With
1、加载10W行数据,自己测试哪个更快?
2、整个表格只有一个期初值?还是每年、每季、每月都有期初?
2、整个表格只有一个期初值?还是每年、每季、每月都有期初?帮助-流水帐-实例-库存表的字段:日期,产品,入库数,出库数,库存数
需求-流水帐-库存表的字段:日期,产品,期初数,入库数,出库数,库存数
库存表能有更优化,每年会更省事,但可能数据会大比如100W行数据,可能考虑,每月会相对好,但每月要迁数据
建议做月结。期初永远放到第一行,这样从第二行开始计算即可
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("出库")
mr("库存") = 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
方法一
Dim drs As List(of DataRow) = DataTables("库存表").Select("", "产品,日期")
Dim prev As String = ""
For i As Integer = 0 To drs.count -1
Dim cur As String = drs(i)("产品")
If cur = prev Then
drs(i)("库存") = drs(i - 1)("库存") + drs(i)("入库") - drs(i)("出库")
drs(i)("期初数") = drs(i - 1)("库存") + drs(i)("入库") - drs(i)("出库")
Else
drs(i)("库存") = drs(i)("入库") - drs(i)("出库")
prev = cur
End If
Next
方法二
Dim nms As New List(Of String)
Dim drs As New List(of DataRow)
With DataTables("表名")
nms = .GetValues("产品")
For Each nm As String In nms '找出每个产品的第一行数据, 添加到集合drs中
drs.Add(.Find("产品 = '" & nm & "'", "[_SortKey]"))
Next
For Each dr As DataRow In drs
.DataCols("入库").RaiseDataColChanged(dr)
.DataCols("期初数").RaiseDataColChanged(dr)
Next
End With
完全不需要期初数这种列,期初值放到第一行的入库列即可。代码不需要做任何改动