Solved

DataGridView: how to save updates to the database.

Posted on 2008-10-01
4
1,337 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 175 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 175 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…

829 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