I use Visual Basic 2008
I use bound Datagridview
I use mysql database


If i run this code, everything on the screen ( in the datagridview) neatly Updated.
But when I look at records in the table "stock" always the first row (0) of the DataGridView not updated in the table "stock" !
All the other record are updated with where in the datagridview!

This always happens!!!

How can fix it?

myAdapter2.SelectCommand = myCommand2
MyBuilder2 = New MySql.Data.MySqlClient.MySqlCommandBuilder(myAdapter2)
MyAdapter2.Fill(MyDataset2, "stock")
MyDataTable2 = MyDataset2.Tables("stock")


 Dim r as integer = 0
For Each Me.MyDataRow2 In MyDataTable2.Rows()
            ViewStock.Item("instock", r).Value() = 1
            r = r + 1
Next

ViewStock.EndEdit()

 Dim RowsAffectedModified2 As Integer
        If MyDataset2.HasChanges(DataRowState.Modified) Then
            RowsAffectedModified2 = myAdapter2.Update(MyDataTable2.Select(Nothing, Nothing, DataViewRowState.ModifiedCurrent))
        Else
            RowsAffectedModified2 = 0
        End If

Dani AI

Generated

This is a very common binding/commit problem rather than a MySQL bug. The usual causes are: (a) the current row/cell was never committed to the DataTable, (b) the code writes into the grid instead of the underlying DataRow (and the grid’s row order may not match the DataTable), or (c) the RowState for that first row isn’t Modified when you call Update. , the quickest reliable fixes are to update the DataRow directly and to ensure any pending edits are ended before calling Update.

Practical fixes you can apply immediately:

  • Update the DataTable rows instead of setting DataGridView cells. That guarantees RowState becomes Modified:

    For Each dr As DataRow In MyDataTable2.Rows
      dr("instock") = 1
    Next
    myAdapter2.Update(MyDataTable2)
  • If you must change the grid, commit the grid+binding edits first:

    ViewStock.CommitEdit(DataGridViewDataErrorContexts.Commit)
    BindingSource1.EndEdit()
    myAdapter2.Update(MyDataTable2)

Troubleshooting tips if the first row still won’t update:

  • Inspect RowState to see why the adapter skips it:
    For Each dr As DataRow In MyDataTable2.Rows
      Debug.Print(dr("id") & " => " & dr.RowState.ToString())
    Next
  • Ensure the DataTable has a proper primary key (CommandBuilder needs it to generate an UPDATE).
  • Don’t call AcceptChanges before Update — that clears RowState and prevents updates.
  • If your grid is sorted/filtered, map via the bound item to avoid index mismatches:
    Dim dv As DataRowView = CType(ViewStock.Rows(0).DataBoundItem, DataRowView)
    dv("instock") = 1
    dv.EndEdit()

’s link may offer related examples, but the core is: commit edits or change the DataRow directly, confirm RowState is Modified, and confirm the adapter has an UpdateCommand (or a primary key) before calling Update.

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.