Link to home
Start Free TrialLog in
Avatar of MBHEY131
MBHEY131

asked on

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"
??????
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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.
Avatar of MBHEY131
MBHEY131

ASKER

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)!
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
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