Solved

Save changes from Datagridview back to dataset

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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