以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  同步外部数据表  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=174903)

--  作者:ls93005
--  发布时间:2022/2/10 17:34:00
--  同步外部数据表
根据外部数据表更新数据,新的增加,少的删除,增加部分已经正常,删除部分为什么无法运行成功?

Dim Ftb As WinForm.Form = Forms("系统数据同步")
Dim wjlj As String = Ftb.Controls("TextBox-wjlj").Value
Dim hblm As String = Ftb.Controls("TextBox-hblm").Value
Dim Book As New XLS.Book(wjlj)
Dim Sheet As XLS.Sheet = Book.Sheets(0)
Dim tb As Table
Dim dtb As DataTable
Dim y As String
Dim zhuj As String

If Ftb.Controls("RadioButton-yh-ky").Checked = True Then
tb = Tables("Yehu_ke")
dtb = DataTables("Yehu_ke")
y = 0
ZhuJ = "序号"
End If

If Ftb.Controls("RadioButton-yh-hy").Checked = True Then
tb = Tables("Yehu_huo")
dtb = DataTables("Yehu_huo")
y = 5
ZhuJ = "许可证号"
End If

tb.StopRedraw()
Dim nms() As String = hblm.Split(",")
Dim dels As New List(of String)
For n As Integer = 1 To Sheet.Rows.Count -1
Dim bh As String = sheet(n,y).Text
dels.Add(bh)
Dim dr As DataRow = dtb.Find(" " & ZhuJ & " = \'" & bh & "\'")
If dr Is Nothing Then \'如果不存在同编号的订单
dr = dtb.AddNew()
End If
For m As Integer = 0 To nms.Length - 1
dr(nms(m)) = Sheet(n,m).Value
Next
Next

For del As Integer = dtb.DataRows.Count - 1 To 0
If dels.Contains(dtb.DataRows(del)(ZhuJ)) = False Then
dtb.DataRows(del).Delete()
End If
Next

tb.ResumeRedraw()


--  作者:有点蓝
--  发布时间:2022/2/10 17:37:00
--  
删除要倒序处理,比如:http://www.foxtable.com/webhelp/topics/0599.htm


--  作者:ls93005
--  发布时间:2022/2/10 17:38:00
--  
For del As Integer = dtb.DataRows.Count - 1 To 0

这个已经是倒序了吧?老师

--  作者:有点蓝
--  发布时间:2022/2/10 20:25:00
--  
For del As Integer = dtb.DataRows.Count - 1 To 0 step -1

我们可以用Step语句将步长设为负值,这样可以倒序来执行,例如:

For i As Integer = 100 To 1 Step -1
    Output.Show(i)

Next


--  作者:ls93005
--  发布时间:2022/2/11 8:12:00
--  
忘记了……少了设置步长。谢谢老师,就是我的这段代码把Excel项加入集合的方法来删除,是否有其他的思路可以更高效呢?
--  作者:有点蓝
--  发布时间:2022/2/11 8:34:00
--  
增加一个辅助的逻辑列,查询有数据或者新增行就勾选,最后使用deletefor删除不勾选的行即可:
--  作者:ls93005
--  发布时间:2022/2/11 8:47:00
--  
If Ftb.Controls("RadioButton-yh-ky").Checked = True Then
    bm ="Yehu_ke"
    tb = Tables(bm)
    dtb = DataTables(bm)
    y = 0
    ZhuJ = "序号"
ElseIf Ftb.Controls("RadioButton-yh-hy").Checked = True Then
    bm = "Yehu_huo"
    tb = Tables(bm)
    dtb = DataTables(bm)
    y = 5
    ZhuJ = "许可证号"
ElseIf Ftb.Controls("RadioButton-yh-wy").Checked = True Then
    bm = "Yehu_wy"
    tb = Tables(bm)
    dtb = DataTables(bm)
    y = 18
    ZhuJ = "经营许可证号"

这种判断语句是否有更简洁的写法?

--  作者:有点蓝
--  发布时间:2022/2/11 9:08:00
--  
dim a1() as string = {"RadioButton-yh-ky","RadioButton-yh-hy"....}
dim a2() as string = {0,5,....}
dim a3() as string = {"序号","许可证号"....}
dim a4() as string = {"Yehu_ke","Yehu_huo",....}
for i as integer = 0 to a1.length - 1
If Ftb.Controls(a1(i)).Checked = True Then
 bm =a4(i)
    tb = Tables(bm)
    dtb = DataTables(bm)
    y =a2(i) 
    ZhuJ = a3(i)
exit for
next