Reputation: 85
I put a DataGridView named DataGridView2
on my form.
And I assigned dataSource
property to a table in my DataSet, called "Account".
I set the default setting for dataGridView which allows user to modify it. And I put a Save button for user to save changed values from the grid to the database. However it doesn't work.
The code which I use in save button is :
Me.Validate()
Me.DataGridView2.Update()
Me.MyDBTableDataset.AcceptChanges()
when I debug it in the second line the there is proper value in :
DataGridView2.Rows(0).Cells(2).Value
which is user modified value in the grid. But when it executes AcceptChanges
and finishes, no changes applied to my database.
Would be great if somebody can help. Thanks
Upvotes: 0
Views: 1287
Reputation: 54487
AcceptChanges
does not do what you think it does. It doesn't save anything. It actually resets the DataSet
so that it contains no changes to save. You can also get rid of that call to the grid's Update
method as it is useless. All it does is redraw the grid onscreen.
What you actually need to do is to call Update
on the same data adapter or table adapter that you originally called Fill
on to retrieve the data in the first place. The Fill
method executes the SelectCommand
to retrieve data and the Update
method executes the InsertCommand
, UpdateCommand
and DeleteCommand
as needed to save changes back to the database.
That Update
method will implicitly call AcceptChanges
to indicate that the changes have been saved. It's rare that you should ever need to call AcceptChanges
explicitly.
Upvotes: 1