Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DataGridView: how to save updates to the database.

Posted on 2008-10-01
4
Medium Priority
?
1,391 Views
Last Modified: 2013-11-26
I'm using Visual Studio 2008, Visual Basic.Net.
I have a DataGridView. It is bound using an adapter. It shows the data, and I've set the properties to allow all updates.
When the user keys in changes, they are just lost when the application closes. They don't commit to the database.
What am I missing? What would be the simplest code to tell the DataGridView to commit changes (update, insets, deletes) to the table?
0
Comment
Question by:mcoleson
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 700 total points
ID: 22619601
0
 
LVL 48

Expert Comment

by:jpaulino
ID: 22619607
This is an example that I have post and uses Microsoft Access as datasource, but you can do the same to SQL or other database.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 700 total points
ID: 22619634
There are many ways to do this, row by row, or at one step. The key is to use a dataadapter.
Have a look to this article: http://www.codeproject.com/KB/database/DataGridView2Db.aspx

if your datagrid is related to a single table, could be done with easy at form's close event, if you have previously filled your datatable as:

SqlDataAdapter da=new SqlDataAdapter("select * from yourtable", yourconn);
SqlCommandBuilder cb=new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource=ds.Tables[0];

then you just have to do:

da.Update();  // copy all the changes to ds to the database
0
 

Author Closing Comment

by:mcoleson
ID: 31502192
Thank you jpaulino and jaime_olivares.
I am now able to update the table from the datagridview.
i just assumed that if I built the datagridview using Microsoft's templates, I could update without overriding the datasource.  Oh well.
Mission accomplished.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

885 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