Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataRowView Delete

Status
Not open for further replies.

SprintFlunky

Programmer
Apr 6, 2004
51
US
Question about the delete functions in a dataview. I am looping through a dataview to validate parent/child relationships. When the parent is not found, I copy the row to a new error table, then delete the row from the dataview.

For Each drv As DataRowView In dvLocMstr
If … "invalid" then
dtRpt.ImportRow(drv.Row)
drv.Delete()
End if
Next

This works great, until I get to the last few rows the for each loop. I get a “can’t find dv row” error. I suspect it’s because I have changed the “row count” by performing the delete, but I don’t accept the changes until I’m out of the loop.

How do I “delete” the row in the dataview without affecting the “for each” loop?

Thanks for any help - this is driving me nuts.
 
Loop through backwards, so any rows removed won't affect the count of those remaining to be checked:

For [red]r As Integer = dvLocMstr.Count - 1 To 0 Step -1[/red]
If … "invalid" then
dtRpt.ImportRow(dvLocMstr(r))
dvLocMstr(r).Delete()
End if
Next

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks for the "head thump", never even thought about that one.

Remind's me of the old saying - couldn't see the forest for the tree's.

 
The one problem I see with the reverse code is that it will reverse iterate through the whole dataset/datatable, not just through the datarows that he may have filtered on, correct? Or only reverse iterate through a few of them from bottom to top since the view.count - 1 will give the correct # of filtered rows.

So if you have a datatable with 500 rows and you filtered that to only 20 rows in a dataview, how would you reverse iterate on just those filtered datarows? I'm asking because I have this problem!

I can iterate from top to bottom on the filtered rows using:

' Use a DataRowView object to only see Filtered rows:

Dim v As DataView = New DataView(t)
Dim dvRow As DataRowView
For Each dvRow In v
'Do some stuff
Next

But for some reason I can't figure out how to reverse just the filtered rows.

Any help would be great.

Thanks,
Dave
 
Note the code for the reverse loop:

For r As Integer = dvLocMstr.Count - 1 To 0 Step -1


The .Count property of the DataView returns the current number of rows, and accounts for the filter. So to use your example, the loop would iterate 20 times for the filtered rows. Again, using your example:

Dim v As DataView = New DataView(t)
Dim dvRow As DataRowView
For [red]r as Integer = v.Count - 1 To 0 Step -1[/red]
'to access the current DataRowView in the loop
[red]dvRow = v(r)[/red]
Next



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
You, sir, are a genius. You are the wind beneath my wings. You.. alright, enough of that.

That worked. Thanks a million!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top