汗,参考代码
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("[日期] = #" & mr("日期") & "# and _Sortkey < " & mr("_Sortkey") & " And [科目编号] = '" & mr("科目编号") & "' and [项目编号] = '" & mr("项目编号") & "'", "日期 desc, _Sortkey desc")
If dr Is Nothing Then '同日期么找到
dr = e.DataTable.Find("[日期] < #" & mr("日期") & "# And [科目编号] = '" & mr("科目编号") & "' and [项目编号] = '" & mr("项目编号") & "'", "日期 desc, _Sortkey desc")
If dr Is Nothing Then
mr("余额") = mr("借方金额") - mr("贷方金额")
dr = mr
Else
mr("余额") = dr("余额") + mr("借方金额") - mr("贷方金额")
End If
Else
mr("余额") = dr("余额") + mr("借方金额") - mr("贷方金额")
End If
drs = e.DataTable.Select("[日期] >= #" & mr("日期") & "# and [科目编号] = '" & dr("科目编号") & "' and [项目编号] = '" & mr("项目编号") & "'", "日期,[_SortKey]")
For i As Integer = 1 To drs.Count - 1
drs(i)("余额") = drs(i-1)("余额") + drs(i)("借方金额") - drs(i)("贷方金额")
Next
msgbox(2)
If e.DataCol.Name = "科目编号" OrElse e.DataCol.name = "项目编号" AndAlso e.OldValue IsNot Nothing AndAlso e.OldValue <> e.NewValue Then
Dim filter As String = ""
If e.DataCol.name = "科目编号" Then
filter = " [科目编号] = '" & e.oldvalue & "' and [项目编号] = '" & mr("项目编号") & "'"
ElseIf e.DataCol.name = "项目编号"
filter = " [科目编号] = '" & mr("科目编号") & "' and [项目编号] = '" & e.OldValue & "'"
End If
If dr IsNot Nothing Then
drs = e.DataTable.Select("[日期] >= #" & mr("日期") & "# And " & filter, "日期,[_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