Updating table from DataGrid

mrRogers

Well-known member
Joined
Jan 23, 2016
Messages
45
Programming Experience
3-5
using VS.2015 design screen and VB, i drop in a Data Grid and use the VS to add the dataSet, binding, and connection. All is good but i can not get the code correct to update changes back to data table. any simple techniques?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,148
Location
Sydney, Australia
Programming Experience
10+
It couldn't be simpler. You'll note that there's some auto-generated code that calls Fill on a table adapter to populate a DataTable from the database in the form's Load event handler. To save the changes from that DataTable back to the database, you simply call Update on that same table adapter. The changes are already in the DataTable because it's bound to your grid. Note that you should also call EndEdit on the BindingSource before calling Update, just in case there's a pending edit in the grid.
 

mrRogers

Well-known member
Joined
Jan 23, 2016
Messages
45
Programming Experience
3-5
I attempted but underlying Access table did not update. Can you see what I am coding wrong? thanks
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


'TODO: This line of code loads data into the 'TestDbDataSet5.tblTest' table. You can move, or remove it, as needed.

Me.TblTestTableAdapter.Fill(Me.TestDbDataSet5.tblTest)

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TblTestBindingSource.EndEdit()

If Me.TestDbDataSet5.HasChanges = True Then

Me.TblTestTableAdapter.Update(TestDbDataSet5.tblTest)

End If

End Sub
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,148
Location
Sydney, Australia
Programming Experience
10+
Most likely the Access database did update and your mistake was looking at the wrong database or the right database at the wrong time. It's a very common mistake.

Firstly, test the value returned by Update. If it's zero then there were no changes to save and if it's not zero then there were changes and they were saved. The only other possible outcome is an exception being thrown. If you find that the value is non-zero then your code is working as it should and you're done.

To learn why you can't see the changes and how to fix that, follow the first link in my signature below.
 
Top Bottom