Foxtable(狐表)用户栏目专家坐堂 → [求助]代码简化?


  共有4138人关注过本帖树形打印复制链接

主题:[求助]代码简化?

帅哥哟,离线,有人找我吗?
cqlpjks
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:407 积分:3194 威望:0 精华:0 注册:2011/2/28 11:57:00
[求助]代码简化?  发帖心情 Post By:2022/3/25 10:15:00 [只看该作者]

DataTables("成绩库").StopRedraw
Dim xuankes() As String = {"化","生","政","地"}
Dim Values() As String = {"考号"}
For Each Value As String In Values
    Tables("成绩库").Cols(Value & "排序").Visible = True
Next
'四科排序
For Each Value As String In Values
    For Each nianji As String In DataTables("成绩库").GetValues("年级", "")
        For Each danwei As String In DataTables("成绩库").GetValues("单位","年级 = '" & nianji & "'" )
            Dim tj As String = value & "排序"
            Dim drs As List(Of DataRow) = DataTables("成绩库").Select("[年级] = '" & nianji & "'And [单位] = '" & danwei & "'","年级代码,单位代码,考场号,座位号") '排序
            For n As Integer = 0 To drs.Count - 1 '遍历所有行
                drs(n)(tj) = n + 1 '四科排序填充
            Next
        Next
    Next
Next
'学科排序
For Each xuanke As String In xuankes
    For Each nianji As String In DataTables("成绩库").GetValues("年级", "")
        For Each danwei As String In DataTables("成绩库").GetValues("单位","年级 = '" & nianji & "'" )
            Dim tj1 As String = xuanke & "排序"
            If xuanke = "化" Then
                Dim dr1s As List(Of DataRow) = DataTables("成绩库").Select("[年级] = '" & nianji & "' And [单位] = '" & danwei & "'And [组合] Like '%" & xuanke & "%'","年级代码,单位代码,化考场号,化座位号")
                For n As Integer = 0 To dr1s.Count - 1 '遍历所有行
                    dr1s(n)(tj1) = n + 1 '化学排序填充
                Next
            ElseIf xuanke = "生" Then
                Dim dr1s As List(Of DataRow) = DataTables("成绩库").Select("[年级] = '" & nianji & "' And [单位] = '" & danwei & "'And [组合] Like '%" & xuanke & "%'","年级代码,单位代码,生考场号,生座位号")
                For n As Integer = 0 To dr1s.Count - 1 '遍历所有行
                    dr1s(n)(tj1) = n + 1 '生物排序填充
                Next
            ElseIf xuanke = "政" Then
                Dim dr1s As List(Of DataRow) = DataTables("成绩库").Select("[年级] = '" & nianji & "' And [单位] = '" & danwei & "'And [组合] Like '%" & xuanke & "%'","年级代码,单位代码,政考场号,政座位号")
                For n As Integer = 0 To dr1s.Count - 1 '遍历所有行
                    dr1s(n)(tj1) = n + 1 '政治排序填充
                Next
            ElseIf xuanke = "地" Then
                Dim dr1s As List(Of DataRow) = DataTables("成绩库").Select("[年级] = '" & nianji & "' And [单位] = '" & danwei & "'And [组合] Like '%" & xuanke & "%'","年级代码,单位代码,地考场号,地座位号")
                For n As Integer = 0 To dr1s.Count - 1 '遍历所有行
                    dr1s(n)(tj1) = n + 1 '地理排序填充
                Next
            Else
            End If
        Next
    Next
Next
Tables("成绩库").Sort = "年级代码,单位代码,考场号,座位号"
DataTables("成绩库").ResumeRedraw

上面红色部分代码能否简化?
'学科排序
For Each xuanke As String In xuankes
    For Each nianji As String In DataTables("成绩库").GetValues("年级", "")
        For Each danwei As String In DataTables("成绩库").GetValues("单位","年级 = '" & nianji & "'" )
            Dim tj1 As String = xuanke & "排序"

                Dim dr1s As List(Of DataRow) = DataTables("成绩库").Select("[年级] = '" & nianji & "' And [单位] = '" & danwei & "'And [组合] Like '%" & xuanke & "%'","年级代码,单位代码,xuanke &"考场号",xuanke &"座位号"")  '用 xuanke 简化"化","生","政","地"

                For n As Integer = 0 To dr1s.Count - 1 '遍历所有行
                    dr1s(n)(tj1) = n + 1 '化学排序填充
                Next
        Next
    Next
Next

代码如何修改?请指教。谢谢!
[此贴子已经被作者于2022/3/25 10:18:01编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110738 积分:563610 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/3/25 10:28:00 [只看该作者]

'学科排序
For Each xuanke As String In xuankes
    For Each nd As String() In DataTables("成绩库").GetValues("年级|单位")
            Dim tj1 As String = xuanke & "排序"
                 Dim dr1s As List(Of DataRow) = DataTables("成绩库").Select("[年级] = '" & nd(0) & "' And [单位] = '" & nd(1) & "'And [组合] Like '%" & xuanke & "%'","年级代码,单位代码," & xuanke & "考场号," & xuanke & "座位号")
                For n As Integer = 0 To dr1s.Count - 1 '遍历所有行
                    dr1s(n)(tj1) = n + 1 '排序填充
                Next
        Next
Next
[此贴子已经被作者于2022/3/25 10:29:11编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
cqlpjks
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:407 积分:3194 威望:0 精华:0 注册:2011/2/28 11:57:00
  发帖心情 Post By:2022/3/25 10:49:00 [只看该作者]

谢谢!

 回到顶部
帅哥哟,离线,有人找我吗?
cqlpjks
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:407 积分:3194 威望:0 精华:0 注册:2011/2/28 11:57:00
  发帖心情 Post By:2022/3/28 10:48:00 [只看该作者]

If e.Form.Controls("年级1").value Is Nothing Then
    MessageBox.Show("请选择或输入统计年级后再执行此操作","中止操作",MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    Return
End If
Dim dt As Table = Tables("成绩库")
Dim Kms() As String = {"考号重排"}
Dim xks() As String = {"化学","生物","政治","地理"}
Dim djs() As String = {"化","生","政","地"}
Dim liem1s() As String = {"单位","单位代码","部别","组合","组合代码","姓名","备注"}
Dim liem2s() As String = {"年级","班","期","编号","考号","班级名称","班级代码"}
Dim liem3s() As String = {"排序","座位号","序号"}
Dim liem4s() As String = {"考场号"}
Dim tjnj As String = e.Form.Controls("年级1").text
For Each xk As String In xks
    Dim dtb As New DataTableBuilder( tjnj &"级"& xk &"考号名册")
    For Each liem1 As String In liem1s
        dtb.AddDef(""& liem1 &"",Gettype(String),8)
    Next
    For Each liem2 As String In liem2s
        dtb.AddDef(""& liem2 &"",Gettype(Double))
    Next
    For j As Integer = 0 To djs.length - 1
        dtb.AddDef(""& djs(j) &"",Gettype(String),"","科目")
    Next
    For Each liem3 As String In liem3s
        For j As Integer = 0 To djs.length - 1
            dtb.AddDef(djs(j) & liem3 ,Gettype(Double),"",liem3)
        Next
    Next
    For Each liem4 As String In liem4s
        For j As Integer = 0 To djs.length - 1
            dtb.AddDef(djs(j) & liem4 ,Gettype(String),"",liem4)
        Next
    Next
    dtb.Build()
Next
For j As Integer = 0 To djs.length - 1
    DataTables( tjnj &"级"& xks(j) &"考号名册").DataRows.Clear()
    Dim f As New Filler
    f.SourceTable = DataTables("成绩库")
    f.DataTable = DataTables( tjnj &"级"& xks(j) &"考号名册")    
    f.Filter = "([组合] Like '%" & djs(j) & "%') And 年级 = '" & tjnj & "'"
    f.Fill()
    For Each xk As String In xks
        Tables( tjnj &"级"& xk &"考号名册").SetHeaderRowHeight(60) '表标题行的行高
        Tables( tjnj &"级"& xk &"考号名册").DefaultRowHeight = 24 '设置默认的行高
        For Each liem1 As String In liem1s
            For Each liem2 As String In liem2s
                For Each liem4 As String In liem4s
                    For Each dj As String In djs
                        Dim lms() As String = {liem1,liem2,dj & liem4,dj}
                        For Each lm As String In lms
                            Tables( tjnj &"级"& xk &"考号名册").Cols(""& lm &"").TextAlign = TextAlignEnum.Center
                        Next
                    Next
                Next
            Next
        Next
        If xk = "化学" Then
            Tables( tjnj &"级"& xk &"考号名册").SetColVisibleWidth("单位|60|单位代码|35|部别|50|年级|35|组合|50|组合代码|35|班|26|期|26|编号|80|姓名|60|考号|80|化|45|化排序|40|化考场号|70|化座位号|45|化序号|45|班级名称|55|班级代码|55")
            Tables( tjnj &"级"& xk &"考号名册").Sort = "单位代码,化考场号,化座位号,化序号"
        ElseIf xk = "生物" Then
            Tables( tjnj &"级"& xk &"考号名册").SetColVisibleWidth("单位|60|单位代码|35|部别|50|年级|35|组合|50|组合代码|35|班|26|期|26|编号|80|姓名|60|考号|80|生|45|生排序|40|生考场号|70|生座位号|45|生序号|45|班级名称|55|班级代码|55")
            Tables( tjnj &"级"& xk &"考号名册").Sort = "单位代码,生考场号,生座位号,生序号"
        ElseIf xk = "政治" Then
            Tables( tjnj &"级"& xk &"考号名册").SetColVisibleWidth("单位|60|单位代码|35|部别|50|年级|35|组合|50|组合代码|35|班|26|期|26|编号|80|姓名|60|考号|80|政|45|政排序|40|政考场号|70|政座位号|45|政序号|45|班级名称|55|班级代码|55")
            Tables( tjnj &"级"& xk &"考号名册").Sort = "单位代码,政考场号,政座位号,政序号"
        ElseIf xk = "地理" Then
            Tables( tjnj &"级"& xk &"考号名册").SetColVisibleWidth("单位|60|单位代码|35|部别|50|年级|35|组合|50|组合代码|35|班|26|期|26|编号|80|姓名|60|考号|80|地|45|地排序|40|地考场号|70|地座位号|45|地序号|45|班级名称|55|班级代码|55")
            Tables( tjnj &"级"& xk &"考号名册").Sort = "单位代码,地考场号,地座位号,地序号"
        Else
        End If
    Next
Next

上面红色部分代码能否简化?

For Each dj As String In djs
    Tables( tjnj &"级"& xk &"考号名册").SetColVisibleWidth("单位|60|单位代码|35|部别|50|年级|35|组合|50|组合代码|35|班|26|期|26|编号|80|姓名|60|考号|80|"& dj &"|45|" & dj & "排序|40|" & dj & "考场号|70|" & dj & "座位号|45|" & dj & "序号|45|班级名称|55|班级代码|55")
    Tables( tjnj &"级"& xk &"考号名册").Sort = "单位代码," & dj & "考场号," & dj & "座位号," & dj & "序号"
Next

代码如何修改?请指教。谢谢!

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110738 积分:563610 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/3/28 10:53:00 [只看该作者]

就是上面最后一段代码的用法呀

 回到顶部
帅哥哟,离线,有人找我吗?
cqlpjks
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:407 积分:3194 威望:0 精华:0 注册:2011/2/28 11:57:00
  发帖心情 Post By:2022/3/28 11:04:00 [只看该作者]

执行结果是:显示当dj = “地” 时的结果。

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  7楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110738 积分:563610 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/3/28 11:08:00 [只看该作者]

贴出完整代码

 回到顶部
帅哥哟,离线,有人找我吗?
cqlpjks
  8楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:407 积分:3194 威望:0 精华:0 注册:2011/2/28 11:57:00
  发帖心情 Post By:2022/3/28 11:11:00 [只看该作者]

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:高中考试考号考室座位号编排-测试.rar


If e.Form.Controls("年级1").value Is Nothing Then
    MessageBox.Show("请选择或输入统计年级后再执行此操作","中止操作",MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    Return
End If
Dim dt As Table = Tables("成绩库")
Dim Kms() As String = {"考号重排"}
Dim xks() As String = {"化学","生物","政治","地理"}
Dim djs() As String = {"化","生","政","地"}
Dim liem1s() As String = {"单位","单位代码","部别","组合","组合代码","姓名","备注"}
Dim liem2s() As String = {"年级","班","期","编号","考号","班级名称","班级代码"}
Dim liem3s() As String = {"排序","座位号","序号"}
Dim liem4s() As String = {"考场号"}
Dim tjnj As String = e.Form.Controls("年级1").text
'Dim tjnj As String = 2019
For Each xk As String In xks
    Dim dtb As New DataTableBuilder( tjnj &"级"& xk &"考号名册")
    For Each liem1 As String In liem1s
        dtb.AddDef(""& liem1 &"",Gettype(String),8)
    Next
    For Each liem2 As String In liem2s
        dtb.AddDef(""& liem2 &"",Gettype(Double))
    Next
    For j As Integer = 0 To djs.length - 1
        dtb.AddDef(""& djs(j) &"",Gettype(String),"","科目")
    Next
    For Each liem3 As String In liem3s
        For j As Integer = 0 To djs.length - 1
            dtb.AddDef(djs(j) & liem3 ,Gettype(Double),"",liem3)
        Next
    Next
    For Each liem4 As String In liem4s
        For j As Integer = 0 To djs.length - 1
            dtb.AddDef(djs(j) & liem4 ,Gettype(String),"",liem4)
        Next
    Next
    dtb.Build()
Next
For j As Integer = 0 To djs.length - 1
    DataTables( tjnj &"级"& xks(j) &"考号名册").DataRows.Clear()
    Dim f As New Filler
    f.SourceTable = DataTables("成绩库")
    f.DataTable = DataTables( tjnj &"级"& xks(j) &"考号名册")
    f.Filter = "([组合] Like '%" & djs(j) & "%') And 年级 = '" & tjnj & "'"
    f.Fill()
    For Each xk As String In xks
        Tables( tjnj &"级"& xk &"考号名册").SetHeaderRowHeight(60) '表标题行的行高
        Tables( tjnj &"级"& xk &"考号名册").DefaultRowHeight = 24 '设置默认的行高
        For Each liem1 As String In liem1s
            For Each liem2 As String In liem2s
                For Each liem4 As String In liem4s
                    For Each dj As String In djs
                        Dim lms() As String = {liem1,liem2,dj & liem4,dj}
                        For Each lm As String In lms
                            Tables( tjnj &"级"& xk &"考号名册").Cols(""& lm &"").TextAlign = TextAlignEnum.Center
                        Next
                    Next
                Next
            Next
        Next
        
        For Each dj As String In djs
            Tables( tjnj &"级"& xk &"考号名册").SetColVisibleWidth("单位|60|单位代码|35|部别|50|年级|35|组合|50|组合代码|35|班|26|期|26|编号|80|姓名|60|考号|80|"& dj &"|45|" & dj & "排序|40|" & dj & "考场号|70|" & dj & "座位号|45|" & dj & "序号|45|班级名称|55|班级代码|55")
            Tables( tjnj &"级"& xk &"考号名册").Sort = "单位代码," & dj & "考场号," & dj & "座位号," & dj & "序号"
        Next
        
    Next
Next
[此贴子已经被作者于2022/3/28 11:12:26编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  9楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110738 积分:563610 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/3/28 11:35:00 [只看该作者]

……
    dtb.Build()
Next
For j As Integer = 0 To djs.length - 1
    Dim xk  As String = xks(j)
    Dim dj As String = djs(j)
    Dim tn As String = tjnj &"级"& xk &"考号名册"
    DataTables(tn).DataRows.Clear()
    Dim f As New Filler
    f.SourceTable = DataTables("成绩库")
    f.DataTable = DataTables(tn)
    f.Filter = "([组合] Like '%" & dj & "%') And 年级 = '" & tjnj & "'"
    f.Fill()
    Tables(tn).SetHeaderRowHeight(60) '表标题行的行高
    Tables(tn).DefaultRowHeight = 24 '设置默认的行高
    For Each liem1 As String In liem1s
        Tables(tn).Cols(liem1 ).TextAlign = TextAlignEnum.Center
    Next
    For Each liem2 As String In liem2s
        Tables(tn).Cols(liem2 ).TextAlign = TextAlignEnum.Center
    Next
    For Each liem4 As String In liem4s
        Tables(tn).Cols(liem4 ).TextAlign = TextAlignEnum.Center
        Tables(tn).Cols(dj & liem4).TextAlign = TextAlignEnum.Center
    Next
    Tables(tn).SetColVisibleWidth("单位|60|单位代码|35|部别|50|年级|35|组合|50|组合代码|35|班|26|期|26|编号|80|姓名|60|考号|80|"& dj &"|45|" & dj & "排序|40|" & dj & "考场号|70|" & dj & "座位号|45|" & dj & "序号|45|班级名称|55|班级代码|55")
    Tables(tn).Sort = "单位代码," & dj & "考场号," & dj & "座位号," & dj & "序号"
Next

 回到顶部
帅哥哟,离线,有人找我吗?
cqlpjks
  10楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:一尾狐 帖子:407 积分:3194 威望:0 精华:0 注册:2011/2/28 11:57:00
  发帖心情 Post By:2022/3/28 12:26:00 [只看该作者]

谢谢!但要删除下面红色代码。
  For Each liem4 As String In liem4s
        Tables(tn).Cols(liem4 ).TextAlign = TextAlignEnum.Center
        Tables(tn).Cols(dj & liem4).TextAlign = TextAlignEnum.Center
  Next

[此贴子已经被作者于2022/3/28 12:26:58编辑过]

 回到顶部