Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problem with dt.Rows.Count

Problem with dt.Rows.Count

Scheduled Pinned Locked Moved Visual Basic
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RichardBerry
    wrote on last edited by
    #1

    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?

    A D 2 Replies Last reply
    0
    • R RichardBerry

      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?

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      Try: For BomRow As Integer = Me.dtPackBom2.Rows.Count-1 to 0 Step -1 This will count backwards through your rows.

      1 Reply Last reply
      0
      • R RichardBerry

        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?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        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

        R 1 Reply Last reply
        0
        • D Dave Kreskowiak

          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

          R Offline
          R Offline
          RichardBerry
          wrote on last edited by
          #4

          Thank you Dave!!!!!!!! That explains a lot of my problems when I see it that way.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups