dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] for loop

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    178

    Resolved [RESOLVED] for loop

    Hi guys,
    I am writing a for loop where I am transferring one record from one datagridview to another datagridview. I am actually removing the row from the source datagridview and this is causing the problem. please see code below.
    Code:
    For i = 0 To dgvlistitemtobout.Rows.Count - 1
                noofrows = dgvitemsout.RowCount
                MsgBox(dgvlistitemtobout.Rows(i).Cells(0).Value)
                If dgvlistitemtobout.Rows(i).Cells(0).Value = True Then
                    If dgvlistitemtobout.Rows(i).Cells(4).Value = 1 Then
                        dgvitemsout.Rows.Insert(noofrows, New Object() {dgvlistitemtobout.Rows(i).Cells(1).Value, dgvlistitemtobout.Rows(i).Cells(2).Value, dgvlistitemtobout.Rows(i).Cells(3).Value, dgvlistitemtobout.Rows(i).Cells(4).Value})
                        dgvitemsout.Rows(noofrows).HeaderCell.Value = (noofrows + 1).ToString
                        dgvlistitemtobout.Rows.RemoveAt(i)
                    End If
                End If
            Next
    Removing the row is causing the problem since it is decreasing the number of rows in the source datagridview. please advise.
    thanks
    Ashley

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

    Re: for loop

    If you want to loop through a collection and remove items then loop backwards, i.e. from the last item to the first. That way, the indexes of the remaining items is not affected by removing one.

    Also, don't ever keep using the same long expression over and over, e.g. dgvlistitemtobout.Rows(i) in your case. If you're going to use the same object over and over then just use the expression to get it once and assign it to a local variable and then use that variable over and over.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    178

    Re: for loop

    ok. thanks sir.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    178

    Re: for loop

    Hi sir,
    I am still having an issue with this for loop. The problem is when at the start of the for loop, for example, the number of rows in my datagridview is 5, so it will count from 0 to 4 right? But when i remove a row, the number of rows is 4 now. but it will count to 4 and there is no row at i=4.
    Hw can i handle that?
    Rgds,
    Ashley

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,909

    Re: for loop

    It's like you didn't even read what I posted.
    Quote Originally Posted by jmcilhinney View Post
    If you want to loop through a collection and remove items then loop backwards, i.e. from the last item to the first. That way, the indexes of the remaining items is not affected by removing one.
    If you're looping backwards then is it going to count from 0 to 4? Is that last to first?

  6. #6
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    739

    Re: for loop

    I wouldn't even bother looking at the actual number of rows.

    I'd just loop while rows.count > 0
    Read row (0)
    Copy row(0) to the other grid
    Remove row (0)
    Loop back to the top.

    That way you'll never need to worry about an index and all the resulting rows in the other grid will be the same order as the original.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2011
    Posts
    178

    Re: for loop

    hi jmcilhinney,
    Yea totally true. Really sorry. It works now.
    Ashley

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