Solved

Save changes from Datagridview back to dataset

Posted on 2009-05-13
5
1,960 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
  • 2
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
bhmahler earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now