Solved

Save changes from Datagridview back to dataset

Posted on 2009-05-13
5
1,965 Views
Last Modified: 2013-11-26
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?

0
Comment
Question by:Mike_Stevens
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
bhmahler earned 500 total points
ID: 24377811
Just loop through each row and set the cell value before you call the update method on the table adapter. Something like this
        Dim order As Integer = 1
        For Each drv As DataGridViewRow In Me.DataGridView1.Rows
            drv.Cells("Rec_ID").Value = 1 'Set this to the master record id
 
            'Set the order value
            drv.Cells("Row_ID").Value = order
            'increment the order
            order += 1
        Next

Open in new window

0
 

Author Comment

by:Mike_Stevens
ID: 24378648
Sounds like a good plan but how do i update the datatable once I have made the changes to each row in the datagridview?
0
 

Author Comment

by:Mike_Stevens
ID: 24378821
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"

0
 
LVL 8

Expert Comment

by:bhmahler
ID: 24379452
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.
0
 
LVL 3

Expert Comment

by:mpeaden2
ID: 24379851
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
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question