设计了分页加载树,计算应收、应付、承兑的余额,代码如下,下载的问题是点击不同的年份都会重新开始计算,没法累加之前年份的数据。
以下内容为程序代码:
1 Select Case e.DataCol.Name
2 Case "单位","日期","应收增加","应收减少","应付增加","应付减少","承兑增加","承兑减少","应收结清","应付结清"
3 Dim dr As DataRow
4 Dim mr As DataRow = e.DataRow
5 Dim drs As List(of DataRow)
6 Dim y As Integer = Date.Today.Year
7 Dim m As Integer = Date.Today.Month
8 Dim dt1 As New Date(y, m, 1)
9 Dim dt2 As New Date(y, m, Date.DaysInMonth(y, m)) '获取本月的最后一天
10 Dim jk As Integer
11 Dim ck As Integer
12 Dim jk1 As Integer
13 Dim ck1 As Integer
14 Dim jk2 As Integer
15 Dim ck2 As Integer
16 dr = DataTables("应收应付").Find("[日期] < '" & mr("日期") & "' And [单位] = '" & mr("单位") & "' and 冲红 = 0", "日期 Desc")
17 If dr Is Nothing Then '如果是第一行,计算结存
18 jk = DataTables("应收应付").SQLCompute("Sum(承兑增加)","日期 < " & mr("日期") & " And 单位 = '" & mr("单位") & "'and 冲红 = 0")
19 ck = DataTables("应收应付").SQLCompute("Sum(承兑减少)","日期 < " & mr("日期") & " And 单位 = '" & mr("单位") & "'and 冲红 = 0")
20 mr("cdlj") = e.DataRow("承兑增加") - e.DataRow("承兑减少") + jk - ck
21 jk1 = DataTables("应收应付").SQLCompute("Sum(应收增加)","日期 < " & mr("日期") & " And 单位 = '" & mr("单位") & "'and 冲红 = 0")
22 ck1 = DataTables("应收应付").SQLCompute("Sum(应收减少)","日期 < " & mr("日期") & " And 单位 = '" & mr("单位") & "'and 冲红 = 0")
23 mr("yslj") = e.DataRow("应收增加") - e.DataRow("应收减少") + jk1 - ck1
24 jk2 = DataTables("应收应付").SQLCompute("Sum(应付增加)","日期 < " & mr("日期") & " And 单位 = '" & mr("单位") & "'and 冲红 = 0")
25 ck2 = DataTables("应收应付").SQLCompute("Sum(应付减少)","日期 < " & mr("日期") & " And 单位 = '" & mr("单位") & "'and 冲红 = 0")
26 mr("yflj") = e.DataRow("应付增加") - e.DataRow("应付减少") + jk2 - ck2
27 dr = mr
28 End If
29 drs = e.DataTable.Select("[日期] >= '" & dr("日期") & "' And [单位] = '" & dr("单位") & "'and 冲红 = 0", "日期")
30 For i As Integer = 1 To drs.Count - 1
31 drs(i)("cdlj") = drs(i-1)("cdlj") + drs(i)("承兑增加") - drs(i)("承兑减少")
32 drs(i)("yslj") = drs(i-1)("yslj") + drs(i)("应收增加") - drs(i)("应收减少")
33 drs(i)("yflj") = drs(i-1)("yflj") + drs(i)("应付增加") - drs(i)("应付减少")
34 Next
35 If e.DataCol.Name = "单位" AndAlso e.OldValue IsNot Nothing AndAlso e.OldValue <> e.NewValue Then '如果修改的是单位列
36 dr = e.DataTable.Find("[日期] < '" & mr("日期") & "' And [单位] = '" & e.OldValue & "' and 冲红 = 0", "日期 Desc") '找出旧单位的上一行
37 If dr Is Nothing Then '如果不存在上一行,那么本行就是旧单位的第一行,由于已经被修改为新单位,需要找出旧单位现在的第一行
38 dr = e.DataTable.Find("[单位] = '" & e.OldValue & "' and 冲红 = 0", "日期") '找出旧单位现在的第一行
39 If dr IsNot Nothing Then '如果找到,计算yslj
40 jk = DataTables("应收应付").SQLCompute("Sum(承兑增加)","日期 < " & mr("日期") & " And 单位 = '" & e.OldValue & "' and 冲红 = 0")
41 ck = DataTables("应收应付").SQLCompute("Sum(承兑减少)","日期 < " & mr("日期") & " And 单位 = '" & e.OldValue & "' and 冲红 = 0")
42 jk1 = DataTables("应收应付").SQLCompute("Sum(应收增加)","日期 < " & mr("日期") & " And 单位 = '" & e.OldValue & "' and 冲红 = 0")
43 ck1 = DataTables("应收应付").SQLCompute("Sum(应收减少)","日期 < " & mr("日期") & " And 单位 = '" & e.OldValue & "' and 冲红 = 0")
44 jk2 = DataTables("应收应付").SQLCompute("Sum(应付增加)","日期 < " & mr("日期") & " And 单位 = '" & e.OldValue & "' and 冲红 = 0")
45 ck2 = DataTables("应收应付").SQLCompute("Sum(应付减少)","日期 < " & mr("日期") & " And 单位 = '" & e.OldValue & "' and 冲红 = 0")
46 dr("cdlj") = dr("承兑增加") - dr("承兑减少") + jk - ck
47 dr("yslj") = dr("应收增加") - dr("应收减少") + jk1 - ck1
48 dr("yflj") = dr("应付增加") - dr("应付减少") + jk1 - ck1
49 End If
50 End If
51 If dr IsNot Nothing Then
52 drs = e.DataTable.Select("[日期] >= '" & dr("日期") & "' And [单位] = '" & dr("单位") & "' and 冲红 = 0", "日期")
53 For i As Integer = 1 To drs.Count - 1
54 drs(i)("cdlj") = drs(i-1)("cdlj") + drs(i)("承兑增加") - drs(i)("承兑减少")
55 drs(i)("yslj") = drs(i-1)("yslj") + drs(i)("应收增加") - drs(i)("应收减少")
56 drs(i)("yflj") = drs(i-1)("yflj") + drs(i)("应付增加") - drs(i)("应付减少")
57 Next
58 End If
59 End If