Problem with dt.Rows.Count
-
Hi In the code below, I'm looping through all the items in dtPackBom2 and for each item I then loop through dtExclusions. If the item in dtPackBom2 exists in dtExclusions then I delete it from dtPackBom2. What happens is that even though the loop is For BomRow As Integer = 0 To Me.dtPackBom2.Rows.Count - 1 after you delete a row, I get an exception saying there is no row at position xxx. I then tried adding the row Me.dtPackBom2.AcceptChanges() after I delete a row, hoping that dtPackBom2.Rows.Count will now see the new reduced number of rows but I still get the same exception. It seems to work if I put: If BomRow = Me.dtPackBom2.Rows.Count - 1 Then Exit For For BomRow As Integer = 0 To Me.dtPackBom2.Rows.Count - 1 If BomRow = Me.dtPackBom2.Rows.Count - 1 Then Exit For For i As Integer = 0 To Me.dtExclusions.Rows.Count - 1 If Me.dtPackBom2.Rows(BomRow).Item(2) = Me.dtExclusions.Rows(i).Item(0) Then Me.dtPackBom2.Rows(BomRow).Delete() Me.dtPackBom2.AcceptChanges() Exit For End If Next 'i Next 'BomRow WHY IS THIS HAPPENING. SURELY THE EXPRESSION Me.dtPackBom2.Rows.Count - 1 IS EVALUATED EACH TIME THE LOOP IS EXECUTED? IS THERE A BETTER WAY TO DO THIS?
-
Hi In the code below, I'm looping through all the items in dtPackBom2 and for each item I then loop through dtExclusions. If the item in dtPackBom2 exists in dtExclusions then I delete it from dtPackBom2. What happens is that even though the loop is For BomRow As Integer = 0 To Me.dtPackBom2.Rows.Count - 1 after you delete a row, I get an exception saying there is no row at position xxx. I then tried adding the row Me.dtPackBom2.AcceptChanges() after I delete a row, hoping that dtPackBom2.Rows.Count will now see the new reduced number of rows but I still get the same exception. It seems to work if I put: If BomRow = Me.dtPackBom2.Rows.Count - 1 Then Exit For For BomRow As Integer = 0 To Me.dtPackBom2.Rows.Count - 1 If BomRow = Me.dtPackBom2.Rows.Count - 1 Then Exit For For i As Integer = 0 To Me.dtExclusions.Rows.Count - 1 If Me.dtPackBom2.Rows(BomRow).Item(2) = Me.dtExclusions.Rows(i).Item(0) Then Me.dtPackBom2.Rows(BomRow).Delete() Me.dtPackBom2.AcceptChanges() Exit For End If Next 'i Next 'BomRow WHY IS THIS HAPPENING. SURELY THE EXPRESSION Me.dtPackBom2.Rows.Count - 1 IS EVALUATED EACH TIME THE LOOP IS EXECUTED? IS THERE A BETTER WAY TO DO THIS?
Try:
For BomRow As Integer = Me.dtPackBom2.Rows.Count-1 to 0 Step -1
This will count backwards through your rows. -
Hi In the code below, I'm looping through all the items in dtPackBom2 and for each item I then loop through dtExclusions. If the item in dtPackBom2 exists in dtExclusions then I delete it from dtPackBom2. What happens is that even though the loop is For BomRow As Integer = 0 To Me.dtPackBom2.Rows.Count - 1 after you delete a row, I get an exception saying there is no row at position xxx. I then tried adding the row Me.dtPackBom2.AcceptChanges() after I delete a row, hoping that dtPackBom2.Rows.Count will now see the new reduced number of rows but I still get the same exception. It seems to work if I put: If BomRow = Me.dtPackBom2.Rows.Count - 1 Then Exit For For BomRow As Integer = 0 To Me.dtPackBom2.Rows.Count - 1 If BomRow = Me.dtPackBom2.Rows.Count - 1 Then Exit For For i As Integer = 0 To Me.dtExclusions.Rows.Count - 1 If Me.dtPackBom2.Rows(BomRow).Item(2) = Me.dtExclusions.Rows(i).Item(0) Then Me.dtPackBom2.Rows(BomRow).Delete() Me.dtPackBom2.AcceptChanges() Exit For End If Next 'i Next 'BomRow WHY IS THIS HAPPENING. SURELY THE EXPRESSION Me.dtPackBom2.Rows.Count - 1 IS EVALUATED EACH TIME THE LOOP IS EXECUTED? IS THERE A BETTER WAY TO DO THIS?
RichardBerry wrote:
SURELY THE EXPRESSION Me.dtPackBom2.Rows.Count - 1 IS EVALUATED EACH TIME THE LOOP IS EXECUTED?
Don't count on it. If you're removing items from a list, it's better to start at the end and work your way back to 0. The way you're doing it now, you'll skip items in the list when you remove one. Say you have 5 items in your list, indexed 0 to 4, and you're going to end up removing item 2:
Index Items ----- ----- 0 "This" 1 "is" 2 "was" 3 "a" 4 "test"
Your counter, BomRow, has the value of 2 when you make a check and decide that you want to remove the item at that index. Now, after you remove the item, the Items.Count now says 4. Great! Who cares... You're array now looks like this after you remove the item, but BEFORE you get to the
Next
statement.Index Items ----- ----- 0 "This" 1 "is" 2 "a" 3 "test"
You finally reach the Next statement and increment BomRow. It's now 3, BUT since the items have moved down the list, you're not checking the item that just got moved to position 2 in the index! You've just skipped an item! It's better to start at the end of the array and work your way back to 0. That way, you won't be skipping any items, no matter how many items you had to start with and how many you remove in each iteration of the loop.
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
RichardBerry wrote:
SURELY THE EXPRESSION Me.dtPackBom2.Rows.Count - 1 IS EVALUATED EACH TIME THE LOOP IS EXECUTED?
Don't count on it. If you're removing items from a list, it's better to start at the end and work your way back to 0. The way you're doing it now, you'll skip items in the list when you remove one. Say you have 5 items in your list, indexed 0 to 4, and you're going to end up removing item 2:
Index Items ----- ----- 0 "This" 1 "is" 2 "was" 3 "a" 4 "test"
Your counter, BomRow, has the value of 2 when you make a check and decide that you want to remove the item at that index. Now, after you remove the item, the Items.Count now says 4. Great! Who cares... You're array now looks like this after you remove the item, but BEFORE you get to the
Next
statement.Index Items ----- ----- 0 "This" 1 "is" 2 "a" 3 "test"
You finally reach the Next statement and increment BomRow. It's now 3, BUT since the items have moved down the list, you're not checking the item that just got moved to position 2 in the index! You've just skipped an item! It's better to start at the end of the array and work your way back to 0. That way, you won't be skipping any items, no matter how many items you had to start with and how many you remove in each iteration of the loop.
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Thank you Dave!!!!!!!! That explains a lot of my problems when I see it that way.