Click here to Skip to main content
15,889,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello
sir
getting error when run the loop and going to last value of row count
please help

and i can not use the 2nd loop in the place of cells(?????) number how can i use this

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


What I have tried:

Dim aa As Integer
       For Z1 = 0 To DataGridView1.RowCount

           If Me.DataGridView1.Rows(Z1).Cells(2).Value = True Then
               aa = 1
           Else
               aa = 0
           End If
           Me.DataGridView1.Rows(Z1).Cells(7).Value = aa
           If Me.DataGridView1.Rows(Z1).Cells(3).Value = True Then
               aa = aa & 1
           Else
               aa = aa & 0
           End If
           Me.DataGridView1.Rows(Z1).Cells(7).Value = aa
           If Me.DataGridView1.Rows(Z1).Cells(4).Value = True Then
               aa = aa & 1
           Else
               aa = aa & 0
           End If
           Me.DataGridView1.Rows(Z1).Cells(7).Value = aa
           If Me.DataGridView1.Rows(Z1).Cells(5).Value = True Then
               aa = aa & 1
           Else
               aa = aa & 0
           End If
           Me.DataGridView1.Rows(Z1).Cells(7).Value = aa
           If Me.DataGridView1.Rows(Z1).Cells(6).Value = True Then
               aa = aa & 1
           Else
               aa = aa & 0
           End If
           Me.DataGridView1.Rows(Z1).Cells(7).Value = aa
       Next
Posted
Updated 31-Jan-19 19:47pm

1 solution

The VB For loop runs from the start to the end value inclusive - so this:
For index As Integer = 0 To 3
    Console.Write(index.ToString & " ")
Next
Will print
0 1 2 3

So your code
For Z1 = 0 To DataGridView1.RowCount
will work from the first index to the one after the last index inclusive, and your app throws an exception. Try:
For Z1 = 0 To DataGridView1.RowCount - 1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900