-
Apr 12th, 2013, 06:26 AM
#1
Thread Starter
Addicted Member
[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
-
Apr 12th, 2013, 06:48 AM
#2
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.
-
Apr 12th, 2013, 06:58 AM
#3
Thread Starter
Addicted Member
-
Apr 16th, 2013, 06:18 AM
#4
Thread Starter
Addicted Member
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
-
Apr 16th, 2013, 06:43 AM
#5
Re: for loop
It's like you didn't even read what I posted.
Originally Posted by jmcilhinney
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?
-
Apr 16th, 2013, 06:54 AM
#6
Fanatic Member
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.
-
Apr 16th, 2013, 07:05 AM
#7
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|