、将窗口的AfterLoad事件设为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | '首先生成统计表 Dim dt As DataTable Dim b As New GroupTableBuilder("统计表1",DataTables("窗口_订单")) b.Groups.AddDef("日期",DateGroupEnum.Year,"年") b.Groups.AddDef("日期",DateGroupEnum.Month,"月") b.Groups.AddDef("产品") b.Totals.AddDef("客户",AggregateEnum.Count,"订单数") '根据客户列来统计记录数,也就是订单数 dt = b.Build(True) '参数设置为True,生成一个临时表,不在主界面显示 '然后根据统计表生成目录树 Dim trv As WinForm.TreeView = e.Form.Controls("TreeView1") trv.BuildTree(dt, "年|月|产品") trv.StopRedraw For Each nd As WinForm.TreeNode In trv.AllNodes Dim Year As Integer = nd.DataRow("年") Dim Month As Integer = nd.DataRow("月") Dim Product As String = nd.DataRow("产品") Select Case nd.Level Case 0 nd.Text = nd.text & "年(" & dt.Compute("Sum(订单数)","年 = " & Year) & "条)" Case 1 nd.Text = nd.text & "月(" & dt.Compute("Sum(订单数)","年 = " & Year & " And 月 = " & Month) & "条)" Case 2 nd.Text = nd.text & "(" & nd.DataRow("订单数") & "条)" End Select Next trv.Nodes.Insert("显示所有行","显示所有行(" & dt.Compute("Sum(订单数)") & "条)",0) trv.ResumeRedraw |
上面的代码中,请留意第8行代码:
dt = b.Build(True) '参数设置为True,生成一个临时表,不在主界面显示
加上参数True后,Build生成的表,不会在主界面显示,当不再有地方引用这个表或这个表中的行和列时,这个表就会被自动销毁,参考不显示生成的表。
以本节的例子来说,由于根据这个表生成了目录树,目录树节点的DataRow属性会引用这个表中的行,所以直到关闭窗口,这个表才会被自动销毁。
6、将目录树的NodeMouseDoubleClick事件设为:
Dim Filter As String = ""
If e.node.Name <> "显示所有行" Then '要用Name属性,不能用Text属性
Dim d1 As Date
Dim d2 As Date
Dim Year As Integer = e.Node.DataRow("年")
Dim Month As Integer = e.Node.DataRow("月")
Dim Product As String = e.Node.DataRow("产品")
Select Case e.Node.Level
Case 0
d1 = New Date(Year,1,1) '取得该年的第一天
d2 = new Date(Year,12,31) '取得该年的最后一天
Filter = "日期 >= #" & d1 & "# And 日期 <= #" & d2 & "#"
Case 1
d1 = New Date(Year, Month, 1) '取得该月的第一天
d2 = new Date(Year, Month, Date.DaysInMonth(Year,Month)) '取得该月的最后一天
Filter = "日期 >= #" & d1 & "# And 日期 <= #" & d2 & "#"
Case 2
d1 = New Date(Year, Month, 1) '取得该月的第一天
d2 = new Date(Year, Month, Date.DaysInMonth(Year,Month)) '取得该月的最后一天
Filter = "日期 >= #" & d1 & "# And 日期 <= #" & d2 & "# And 产品 = '" & Product & "'"
End Select
End If
Tables("订单").Filter = Filter
1~8行改为使用第三方组件加载表格后绑定到窗口表
第11行【trv.BuildTree(dt, "年|月|产品")】改为使用窗口表【trv.BuildTree(datatables("窗口1_table1"), "年|月|产品")】
其它基本不变
t2.AllowEdit = False 'sqlta
Dim dt As DataTable
Dim trv3 As WinForm.TreeView = e.Form.Controls("TreeView4")
trv.BuildTree(DataTables("窗口_Table1"), "年|月|产品")
trv3.StopRedraw
For Each nd As WinForm.TreeNode In trv.AllNodes
Dim Year As Integer = nd.DataRow("年")
Dim Month As Integer = nd.DataRow("月")
Dim Product As String = nd.DataRow("品名")
Select Case nd.Level
Case 0
nd.Text = nd.text & "年(" & dt.Compute("Sum(订单数)", "年 = " & Year) & "条)"
Case 1
nd.Text = nd.text & "月(" & dt.Compute("Sum(订单数)", "年 = " & Year & " And 月 = " & Month) & "条)"
Case 2
nd.Text = nd.text & "(" & nd.DataRow("订单数") & ")"
End Select
Next
trv3.Nodes.Insert("加载所有行", "加载所有行(" & dt.Compute("Sum(订单数)") & "条)", 0)
trv3.ResumeRedraw
执行报错,年无效,如何解决?
[此贴子已经被作者于2023/11/23 18:00:18编辑过]
去百度,mysql怎么在sql里返回日期列的年月日
Dim dt As DataTable
Dim Sql-3 = "SELE-CT DISTINCT Year(日期) As 年, Month(日期) As 月, 品名 From 窗口_Table1 "
Dim trv3 As WinForm.TreeView = e.Form.Controls("TreeView4")
trv.BuildTree(DataTables("窗口_Table1"), "年|月|产品")
trv3.StopRedraw
For Each nd As WinForm.TreeNode In trv.AllNodes
Dim Year As Integer = nd.DataRow("年")
Dim Month As Integer = nd.DataRow("月")
Dim Product As String = nd.DataRow("品名")
Select Case nd.Level
Case 0
nd.Text = nd.text & "年(" & dt.Sq-l3("Sum(订单数)", "年 = " & Year) & "条)"
Case 1
nd.Text = nd.text & "月(" & dt.Sq-l3("Sum(订单数)", "年 = " & Year & " And 月 = " & Month) & "条)"
Case 2
nd.Text = nd.text & "(" & nd.DataRow("订单数") & ")"
End Select
Next
trv3.Nodes.Insert("加载所有行", "加载所有行(" & dt.Compute("Sum(订单数)") & "条)", 0)
trv3.ResumeRedraw
报错:nd.Text = nd.text & "年(" & dt.Sq-l3("Sum(订单数)", "年 = " & Year) & "条)"不是DataTable