简体   繁体   English

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

[英]how to delete row when u loop on datatable

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

It is giving error because when you changed something in datatable, its index get changed.它给出了错误,因为当您更改数据表中的某些内容时,其索引会更改。

You won't be able to do this in a For Each loop because when you remove something, the collection has changed and you can't enumerate it anymore.您将无法在 For Each 循环中执行此操作,因为当您删除某些内容时,集合已更改并且您无法再枚举它。

What you need is a reversed For loop.你需要的是一个反向的 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

This will still work even as you remove rows because the ones you're not touching aren't having their indexes changed and it doesn't use an enumeration.即使您删除行,这仍然有效,因为您没有接触的行没有更改其索引并且它不使用枚举。

Sai, it fails because you should not really delete the rows while looping in all table rows. Sai,它失败了,因为你不应该在所有表行中循环时真正删除行。

an example of what you could do, in C# is this one:你可以做什么的一个例子,在 C# 中是这样的:

   DataTable dt = CreateDataSource();

    DataRow[] rows = (from t in dt.AsEnumerable().Cast<DataRow>()
                      where t.Field<int>("ID") == 1
                      select t).ToArray();

    foreach (DataRow row in rows)
    {
        dt.Rows.Remove(row);
    }

so as you see you first select all rows to delete with LINQ then you loop only on the resulting rows and remove them from the source table.因此,如您所见,您首先使用 LINQ 选择要删除的所有行,然后仅在结果行上循环并将它们从源表中删除。

sorry no time to write this in VB.NET but the idea should be clear I hope.抱歉没有时间在 VB.NET 中写这个,但我希望这个想法应该很清楚。

My problem was that I needed to loop a table multiple times, so after deleting rows in the first round it would throw an exception when coming to the index of a previously deleted row.我的问题是我需要多次循环一个表,所以在第一轮删除行后,它会在到达先前删除的行的索引时抛出异常。 I ended up cloning the table and copying into it the rows I wanted to keep.我最终克隆了表格并将我想要保留的行复制到其中。

Dim NewInvoiceDT as DataTable = InvoiceDT.clone
For Each Dr As DataRow In InvoiceDT.Rows
    If 'some statement
        Dim NewDr as DataRow = NewInvoiceDT.Rows.Add
        NewDr.ItemArray = Dr.ItemArray
    End if  
Next

To iterate through a DataTable and remove rows conditionally (eg: row having column_id as 16), you can use the following code:要遍历 DataTable 并有条件地删除行(例如:column_id 为 16 的行),您可以使用以下代码:

Dim iterateIndex As Integer = 0    
Dim newDataTable As DataTable = originalDataTable.Copy
For Each row As DataRow In newDataTable.Rows
   If row("column_id") = 16 Then
      originalDataTable.Rows.RemoveAt(iterateIndex)
   Else
      iterateIndex += 1
   End If
Next

It's simple这很简单

For i = 0 To dt.Rows.Count - 1
'DO ....  then delete....
dt.Rows(0).Delete()
Next

您不能使用简单的DELETE语句进行DELETE吗?

DELETE FROM xxx WHERE Amount = yyy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM