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
dr = e.DataTable.Find("日期 < #" & mr("日期") & "# and [_Identify] < " & mr("_Identify") & " And [项目名称] = '" & mr("项目名称") & "'", "日期 Desc,[_Identify] Desc")
If dr Is Nothing Then '如果是第一行,计算结存
jk = DataTables("表A").SQLCompute("Sum(收入)","日期 < #" & mr("日期") & "# and [_Identify] < " & mr("_Identify") & " And 项目名称 = '" & mr("项目名称") & "'")
ck = DataTables("表A").SQLCompute("Sum(支出)","日期 < #" & mr("日期") & "# and [_Identify] < " & mr("_Identify") & " And 项目名称 = '" & mr("项目名称") & "'")
mr("余额") = e.DataRow("收入") - e.DataRow("支出") + jk - ck
dr = mr
End If
drs = e.DataTable.Select("日期 >= #" & dr("日期") & "# and [_Identify] >= " & dr("_Identify") & " And [项目名称] = '" & dr("项目名称") & "'", "日期,[_Identify]")
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("日期 < #" & mr("日期") & "# and [_Identify] < " & mr("_Identify") & " And [项目名称] = '" & e.OldValue & "'", "日期 Desc,[_Identify] Desc") '找出旧项目名称的上一行
If dr Is Nothing Then '如果不存在上一行,那么本行就是旧项目名称的第一行,由于已经被修改为新项目名称,需要找出旧项目名称现在的第一行
dr = e.DataTable.Find("[项目名称] = '" & e.OldValue & "'", "日期,[_Identify]") '找出旧项目名称现在的第一行
If dr IsNot Nothing Then '如果找到,计算余额
jk = DataTables("表A").SQLCompute("Sum(收入)","日期 < #" & mr("日期") & "# and [_Identify] < " & mr("_Identify") & " And 项目名称 = '" & e.OldValue & "'")
ck = DataTables("表A").SQLCompute("Sum(支出)","日期 < #" & mr("日期") & "# and [_Identify] < " & mr("_Identify") & " And 项目名称 = '" & e.OldValue & "'")
dr("余额") = dr("收入") - dr("支出") + jk - ck
End If
End If
If dr IsNot Nothing Then
drs = e.DataTable.Select("日期 >= #" & dr("日期") & "# and [_Identify] >= " & dr("_Identify") & " And [项目名称] = '" & dr("项目名称") & "'", "日期,[_Identify]")
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