Hey I need help from danniweb .
I try some codes according to my knowledge to make this code.
I load some valuse to data grid view from mysql table. Think there are 10 records loaded to data grid view. Then just I want to make 10 records to generate code.
I'm trying this: But it wont help.

    Dim counter As Integer
        Dim i As Integer
        Dim rec As Integer = 100
        For counter = 0 To (DataGridView1.Rows.Count - 1)
            'MessageBox.Show(counter)
            DataGridView3.Rows(i).Cells(0).Value() = counter
            'DataGridView3.Rows(i).Cells(0).Value() = rec + counter
        Next

In this code make this, >>> code retrive how many records on data grid view and count. then number of records+100 and put 104 answer as first record. just that. But I want to put it one by one down.

Dani AI

Generated

The immediate cause of the problem in ’s snippet is an indexing bug: the loop uses i but never assigns it (as already pointed out). Two other common pitfalls make this kind of loop behave unexpectedly: indexing into DataGridView3 when it has no rows yet, and counting the built‑in "new row" (when AllowUserToAddRows = True). Below are two practical, robust patterns — one for unbound grids and one for bound DataTables — plus a few quick troubleshooting notes.

' Unbound grids: create rows in DataGridView3 as needed and assign sequential codes
Dim baseNumber As Integer = 100
Dim outIndex As Integer = 0

For Each srcRow As DataGridViewRow In DataGridView1.Rows
    If srcRow.IsNewRow Then Continue For
    If outIndex >= DataGridView3.Rows.Count Then
        DataGridView3.Rows.Add()
    End If
    DataGridView3.Rows(outIndex).Cells("Code").Value = baseNumber + outIndex + 1
    outIndex += 1
Next
' Bound grid (DataTable): update the underlying table so the grid stays in sync
' assume dt is the DataTable bound to DataGridView1 and has a "Code" column
Dim baseNumber As Integer = 100
For i As Integer = 0 To dt.Rows.Count - 1
    dt.Rows(i)("Code") = baseNumber + i + 1
Next
' if using a BindingSource, changes appear automatically

Troubleshooting notes: prefer column names ("Code") over numeric indexes to avoid off‑by‑one errors; check IsNewRow or compute actual row count by filtering out new rows; if numbering direction should be reversed (first row = base + totalRows) use baseNumber + (totalRows - i); enable Option Explicit/Option Strict to catch undeclared/typed variables at compile time. If numbering should live in the query, add a computed column (e.g., ROW_NUMBER() or the appropriate MySQL construct) on the server side.

You use i in line 6 but it is not defined.

What is happening when yu run ?

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.