Delete Rows from Dataset

I am trying to delete rows from a Dataset based on a certain condition being met.  I am using the following code which validates the condition but not all of the rows that show be deleted are deleted.  I am hoping someone can shed some light on what I am doing wrong.

For i As Integer = 0 To ds.Tables("search_result").Rows.Count - 1

               Dim dr As DataRow = ds.Tables("search_result").Rows(i)
           
               If Check_Status(dr("IDNum")) = False Then
                        dr.Delete()
               End If
               
Next i

                ds.Tables("search_result").AcceptChanges()
Mike_StevensAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Moe DeShongCommented:
I didn't try this but my guess is when you delete a row you are changing the row count so some of the rows are being skipped.  You should probably mark the row for deletion in some way then come back and delete all the marked rows at once.  Maybe programatically add a boolean column and set it to true if it needs deleted.
0
Tommy11bCommented:
When you delete a row from a datatable or dataset, it marks the RowState appropiately, So in this case your marking these rows as "deleted". After this you would then call an update method to make the changes in your actual database. HOWEVER by explicitly calling the AcceptChanges method of your dataset/datatable, you are changing the RowState on each row back to "Unmodified".
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Mike_StevensAuthor Commented:
The changes that I am making are dont have to be save back to a dataset.   What i am doing is populating a dataset with records and then if certain records meet a specific criteria i want to remove the from the dataset populate a datagridview with the records in the dataset.
0
Tommy11bCommented:
Ok, in that case just remove the AcceptChanges line which is essientially Un-deleting your datarow records. If that still doesnt work, try the remove method instead of delete.
0
Mike_StevensAuthor Commented:
I removed the acceptchanges with no lock.  cant use remove on datarow
0
Tommy11bCommented:
If you step thru your code, are you sure any of the rows are being deleted? I would set a break point on Line:  dr.Delete(), just to confirm the delete to start.
0
Tommy11bCommented:
Also your deleting a variable of the datarow, so not sure if that will be persisted back to your dataset or not. I would do the delete straight from ds.Tables("search_result").Rows(i)
0
Mike_StevensAuthor Commented:
Yes...the delete function is deleting the rows that match the criteria for rows that should be deleted.
0
Tommy11bCommented:
Just guessing but I'm wondering if you have multiple datasets, where your deleting from one dataset but have the datagridview results bound to a different one.
0
Mike_StevensAuthor Commented:
Yes...the rows are being deleted
0
Tommy11bCommented:
Can you attach an example, so I can take a crack at it. Preferably with an xml file since I dont have your db access...
0
Mike_StevensAuthor Commented:
The dataset set is being populated correctly...the issue is with removing select rows.  He is the code that does that.

  For i As Integer = 0 To ds.Tables("search_result").Rows.Count - 1

              If Check_Status(ds.Tables("search_result").Rows(0).Item("IDNum").ToString) = True Then
                    ds.Tables("search_result").Rows(i).Delete()
              End If

 Next
0
CodeCruiserCommented:
I have not read the whole thread but i think you need to change

If Check_Status(ds.Tables("search_result").Rows(0).Item("IDNum").ToString) = True Then

to
If Check_Status(ds.Tables("search_result").Rows(i).Item("IDNum").ToString) = True Then

Notice that Rows(0) has changed to Rows(i)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.