Datagredview row selection

here's my code:
        For i = 1 To 4
            j = 0
            If IsDBNull(Me.Dtabse1_Discounts_DataSet1.Tables("RO_Inv_Discounts").Rows(0).Item( _
                "RO_TtlDiscText" + CStr(i))) Then
            Else
                For Each row As DataGridViewRow In DGV_Disc.Rows
                    Dim chkboxcell As DataGridViewCheckBoxCell = row.Cells("Disc_AddToRO")
                    If IsDBNull(Me.DiscDataSet.Tables("DscntTbl").Rows(j).Item("Disc_Name")) Then
                    Else

                        If (Me.DiscDataSet.Tables("DscntTbl").Rows(j).Item("Disc_Name")) = (Me.Dtabse1_Discounts_DataSet1.Tables("RO_Inv_Discounts").Rows(0).Item( _
                            "RO_TtlDiscText" + CStr(i))) Then
                            chkboxcell.Value = True
                        Else
                        End If
                    End If
                    j = j + 1
                Next
            End If
        Next
]

Open in new window


My Question is
when I define the "row" in my datagridview that is filled with "8" lines of data (this I have looked at and know for a fact)
that VARIABLE "row" starts life out  at "zero" (WHICH IS FINE) which in my way of thinking it should EXIT the FOR NEXT at 7
but is doesn't (it exits after processing 8) which to me means its processing 9 lines of data and 9 lines don't exist????


I also dim a "j" variable and start it's also starts life out as a "0"

but the FOR NEXT Loop doesn't stop until it processes an 8 and I get an ERROR because there is no row 8 for my variable "j"
??????
MBHEY131Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Collections are zero based indexing - so an index of 0 is the first row.  Something you have noticed.

Looking at your code you increment your 'j' variable each time through the loop.  So after processing one row it has the value one, two rows and is equal to two .... eight rows and is equal to eight.
Unless I totally misunderstand everything is working correctly.
MBHEY131Author Commented:
yes I think I understand what your saying and yes it is correct - but my question is why does the "row" variable get processed 9 times when there is only 8 rows of data in the dataset? once it (the variable "row" reaches " the value of 7" it should be done?)
 once my variable "j" reaches 8 the FOR NEXT LOOP should be terminated! and it should not process my variable "j" when it has the value of (8)!
AndyAinscowFreelance programmer / ConsultantCommented:
For Each row As DataGridViewRow In DGV_Disc.Rows
***process row***
                    j = j + 1
                Next

After row number zero is done j equals one
After row number one is done j equals two
....
After row number seven (the final row) is done j equals eight

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MBHEY131Author Commented:
yes - you are correct and when the "After row number seven (the final row) is done j equals eight " the FOR NEXT LOOP SHOULD TERMINATE but it doesn't it continues on and processes the row when the value is 8 for the variable row the fact that it (the variable "J" gets to 8 it SHOULDN'T) BE PROCESSED as is my understanding!
MBHEY131Author Commented:
you got me thinking in a different direction and it turned out to be the correct direction in the I had (in my case) to set the "AllowUserToAddRows property of your DataGridView control to False:" run the code and then set the property back to "TRUE" which is where I want it set
All is functioning well and much joy
Thanx
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.