7
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.

6 Answers 6

21

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.

What you need is a reversed For loop.

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.

Sign up to request clarification or add additional context in comments.

7 Comments

ur code is right. but when we use this it will show data in descending order
If it does that, you can reverse the order pretty easily afterwards to fix it. :) Also remember if a post answers your question use the checkmark to mark it off as the answer. That encourages more people to help you later. :)
how can i reverse the order? without loop
Without a loop? I don't think you can. I don't see why it'd reverse the list though unless your "some code" area is doing something to the list that's causing that. Just removing stuff won't reverse it.
Might be slightly more elegant to use InvoiceDT.Rows.RemoveAt(i) in the Else clause.
|
5

Sai, it fails because you should not really delete the rows while looping in all table rows.

an example of what you could do, in C# is this one:

   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.

sorry no time to write this in VB.NET but the idea should be clear I hope.

3 Comments

This does not work. Throws an "collection cannot be modified" error
Hi Ajit, not true as we loop on the array but we delete from the table, it worked, at least, in August 2011 when i posted this answer.
You could be right, I missed the ToArray() part which creates a new instance. Stackoverflow is not allowing me to upvote the answer.
2

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

1 Comment

To make a duplicate of a a DataTable, you can use DataTable.Copy. Find official doc here
0

To iterate through a DataTable and remove rows conditionally (eg: row having column_id as 16), you can use the following code:

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

Comments

-1

It's simple

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

Comments

-2

Can you not delete using a simple DELETE statement?

DELETE FROM xxx WHERE Amount = yyy

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.