Solved

DataGridView: how to save updates to the database.

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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