dcsimg
Results 1 to 8 of 8

Thread: Is it possible to update the previous row in a for each loop?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    71

    Is it possible to update the previous row in a for each loop?

    I have a datatable I need to be sure I have no duplicates in and update fields accordingly. It there is a duplicate, I need to make sure a field is updated.

    The scenario is I have parts with an onhand qty. We may have duplicate parts in different locations so I need to iterate through the datatable and delete the duplicate part but update the onhand value.

    Code Below:
    Code:
                Dim part, partwas As String
                Dim onhndqty As Double = 0
                partwas = ""
    
                For Each r In OnHand.Rows
                    part = r.item("fpartno")
    
                    If part = partwas Then
                        'Dup part get onhand qty and delete it
                        onhndqty = r.item("fonhand")
                        'Update the previous record
    
                        'This is where I need help
                        r -1.item("fonhand") = r - 1.item("fonhand") + onhndqty
    
    
                        'Delete the row
                        r.delete()
                    End If
    
                    partwas = part
                Next
    Thanks all.
    Last edited by Shaggy Hiker; Mar 2nd, 2018 at 09:45 AM. Reason: Added CODE tags.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,608

    Re: Is it possible to update the previous row in a for each loop?

    Use a For loop rather than a For Each loop. If you're getting items by index, it's then very easy to get the previous item. You simply subtract 1 from the current index.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    71

    Re: Is it possible to update the previous row in a for each loop?

    Do you have an example?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,748

    Re: Is it possible to update the previous row in a for each loop?

    For i = 0 to OnHand.Rows.Count-1


    That's the main change. Of course, that would mean that r would no longer exist, so some other rows would have to change like:

    part = r.item("fpartno")

    would become:

    part = Onhand.rows(i).item("fpartno")

    The previous row would be:

    Onhand.Rows(i-1)
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    71

    Re: Is it possible to update the previous row in a for each loop?

    I just go it this is what I did.

    Dim r As DataRow
    For i = 0 To OnHandAll.Rows.Count
    r = OnHandAll.Rows(i)

    part = r.Item("fpartno")

    If part = partwas Then
    'Dup part get onhand qty and delete it
    onhndqty = r.Item("fonhand")
    'Update the previous record
    OnHandAll.Rows(i - 1).Item("fonhand") = OnHandAll.Rows(i - 1).Item("fonhand") + onhndqty

    'Delete the row
    r.Delete()
    End If

    partwas = part
    Next

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,748

    Re: Is it possible to update the previous row in a for each loop?

    Almost, except that you can't loop to OnHandAll.Rows.Count. Because you are starting at 0 (which you have to do), then you need to loop to OnHandAllRows.Count -1. If you leave it as you have it, then you'll get an IndexOutOfRange exception on the last time through the loop.
    My usual boring signature: Nothing

  7. #7
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    680

    Re: Is it possible to update the previous row in a for each loop?

    I would probably use a bindingsource

    Something to play with...
    Code:
            Dim i = 0
            Do Until i = BS.Count
                If i > 1 Then
                    CType(BS(i - 1), DataRowView)("Col1") = "Some Stuff"
                End If
                i += 1
                BS.RemoveCurrent() 'not real sure if this will auto MoveNext
                BS.MoveNext() 'may not need
            Loop
            BS.EndEdit()

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2016
    Posts
    71

    Re: Is it possible to update the previous row in a for each loop?

    Right

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width