Link to home
Start Free TrialLog in
Avatar of Mike_Stevens
Mike_StevensFlag for United States of America

asked on

Save changes from Datagridview back to dataset

I have a windows form that I am populating from a MS Access database using a datatable.   I am able to get the data from the database and populate the gridview without any problems.  

The question I have is I need to save the changes that the user makes to the Gridview back to the database.  I know that all I have to do is update the dataadapter.   My issue is that all of the records in the gridview are sub-records of the main record that is appearing on the form.   When I save the record I need to be able to assign the master Rec_ID number to a ID column for each record in the gridview datatable.   I also want to assign the row number to the Row_ID column in the datatable so that the records always appear in the same row.    How can I update these two values (Rec_ID & Row_ID) when saving the data table?

ASKER CERTIFIED SOLUTION
Avatar of bhmahler
bhmahler
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike_Stevens

ASKER

Sounds like a good plan but how do i update the datatable once I have made the changes to each row in the datagridview?
When i try to update the data adapter I get the following message:

"Update requires a valid updatecommand when passed datarow collection with modified rows"

You need to go to your DataSet Designer and verify that the TableAdapter has a valid update command.  If there is not one present you can right click on the table int he designer and select Configure.  This will prompt your for a few things and then will build the necessary queries.  Or you can manually create the Update Statement form the properties window.
If you must use the designer... your bet bet will be to first make sure your tables are setup correctly in your database (Integrity is key!). Select both parent and child tables from the wizard. next drag both tables on the form this will create all the necessary managers, adapters, binders, ect.. doing it this way it will automatically create your update, delete, and insert statements for you... as opposed to trying to add tables and use joins in the designer.  I use vb.net 2008 and the designer is VERY picky about whether or not it creates the statements for you.. I have found the above to be the most consistent way.
Anyway all you have to do in your code is call the .update method and your done