好了,可以实现了,只是调整了一下顺序,就正常了,谢谢老大的提示!
Select Case e.DataCol.Name
Case "借方发生额","贷方发生额"
For Each dr As DataRow In e.DataTable.Select("[_SortKey] >= " & e.DataRow("_SortKey"))
Dim Val1 As Double = e.DataTable.Compute("Sum(借方发生额)","[_SortKey] <= " & dr("_SortKey"))
Dim Val2 As Double = e.DataTable.Compute("Sum(贷方发生额)","[_SortKey] <= " & dr("_SortKey"))
dr("余额") = Val1 - Val2
Next
End Select
Select Case e.DataCol.Name
Case "借方发生额","贷方发生额"
For Each dr As DataRow In e.DataTable.Select("[_SortKey] >= " & e.DataRow("_SortKey"))
Dim Val1 As Double = e.DataTable.Compute("Sum(借方发生额)","[_SortKey] <= " & dr("_SortKey"))
Dim Val2 As Double = e.DataTable.Compute("Sum(贷方发生额)","[_SortKey] <= " & dr("_SortKey"))
dr("明细余额") = Val1 - Val2
Next
Case "明细余额"
If e.newvalue = 0 Then
e.DataRow("余额方向") = "平"
ElseIf e.newvalue > 0 Then
e.DataRow("余额方向") = "借"
Else
e.DataRow("余额方向") = "贷"
End If
End Select
'明细余额列数据DataColChanged事件代码改为:
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
'科目余额列数据DataColChanged事件代码改为:
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
'例如
'Select Case e.DataCol.Name
' Case "借方发生额","贷方发生额"
' For Each dr As DataRow In e.DataTable.Select("[_SortKey] >= " & e.DataRow("_SortKey"))
' Dim Val1 As Double = e.DataTable.Compute("Sum(借方发生额)","[_SortKey] <= " & dr("_SortKey"))
' Dim Val2 As Double = e.DataTable.Compute("Sum(贷方发生额)","[_SortKey] <= " & dr("_SortKey"))
' dr("明细余额") = Val1 - Val2
' Next
' Case "明细余额"
' If e.newvalue = 0 Then
' e.DataRow("余额方向") = "平"
' ElseIf e.newvalue > 0 Then
' e.DataRow("余额方向") = "借"
' Else
' e.DataRow("余额方向") = "贷"
' End If
'End Select