加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

.net – 当你在数据表上循环时如何删除行

发布时间:2020-12-17 07:26:33 所属栏目:百科 来源:网络整理
导读:For Each Dr As DataRow In InvoiceDT.Rows Dim DrResult As Array = PaymentInvoiceDT.Select("Amount='" Dr("Amount").ToString() "'") If DrResult.Length 0 Then ''some code Else InvoiceDT.Rows.remove(Dr) End IfNext 它给出了错误,因为当您在datatab
For Each Dr As DataRow In InvoiceDT.Rows
    Dim DrResult As Array = PaymentInvoiceDT.Select("Amount='" & Dr("Amount").ToString() & "'")
    If DrResult.Length > 0 Then
        ''some code
    Else
        InvoiceDT.Rows.remove(Dr) 
    End If
Next

它给出了错误,因为当您在datatable中更改某些内容时,其索引会发生更改.

解决方法

您将无法在For Each循环中执行此操作,因为当您删除某些内容时,该集合已更改,您无法再枚举它.

你需要的是一个反向的For循环.

For i as Integer = Invoice.Rows.Count -1 to 0 Step -1
    Dim DrResult As Array = PaymentInvoiceDT.Select("Amount='" & Invoice.Rows(i).("Amount").ToString() & "'")
    If DrResult.Length > 0 Then
        'some code
    Else
        InvoiceDT.Rows.remove(InvoiceDT.Rows(i)) 
    End If
Next

即使在删除行时,这仍然有效,因为您没有触摸的行没有更改其索引并且不使用枚举.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读