Solved

Save changes from Datagridview back to dataset

Posted on 2009-05-13
5
1,963 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

735 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