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

Dani AI

Generated

The error means your code is trying to access an index that does not exist — either a row index past the last row or a column index that isn't present. A common DataGridView-specific cause is the built-in "new row" placeholder when AllowUserToAddRows is True; iterating to RowCount (or Rows.Count) without accounting for that extra row produces an off-by-one. 's suggestion to stop one earlier fixes the simple case, but a safer pattern is to iterate rows, skip the new-row placeholder, and verify columns and cell values before using them.

Private Function ToBool(v As Object) As Boolean
    If v Is Nothing OrElse v Is DBNull.Value Then Return False
    Return Convert.ToBoolean(v)
End Function

For Each r As DataGridViewRow In DataGridView1.Rows
    If r.IsNewRow Then Continue For
    If DataGridView1.Columns.Count <= 7 Then Continue For

    Dim total As Integer = 0
    For c As Integer = 2 To 6
        If r.Cells.Count > c AndAlso ToBool(r.Cells(c).Value) Then total += 1
    Next
    r.Cells(7).Value = total
Next

Quick tips: do not use & for numeric addition (it concatenates strings in VB); enable Option Strict to catch type issues; if the grid is data-bound update the underlying data source instead of writing directly to Cells(...); if you do need a numeric For loop stop at Rows.Count - 1 and still skip IsNewRow; set AllowUserToAddRows = False when the new-row placeholder is not wanted.

Try

For Z1 = 0 To DataGridView1.RowCount - 1

If you have 5 rows, for example, the indices are 0, 1, 2, 3, 4

commented: "Hey that's my luggage combination" - Programmer President Skroob. +15
commented: thanks sir +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.