Solved

DataGridView: how to save updates to the database.

Posted on 2008-10-01
4
1,304 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
Comment Utility
0
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Expando 4 33
.Net application crashing 6 19
Create a form which is copy of a form in vb.net 2 17
VB.NET HttpWebRequest 12 30
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

15 Experts available now in Live!

Get 1:1 Help Now