Using and updating a DatagridView

I have a Windows App with a DataGridView.  I would like to give users the ability to update from the datagridview and have the program immediately update the sql server table.  

I have created a .xsd.  Now I am wondering which events to use to validate and update the database, wheter to enable/disable the validating events?

My questions related to this are:

On which events should I be editing the changed data?
On which event should I update the database?

How will I need to keep track when user has changed a key column and an update would no longer work?


Get me started, please  
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

garyinmiami2003Author Commented:

Your comment was helpful but not what I'm looking for.  Why:

I know that c# and are very similar.  Your solution is c#, my need is  Also, I want to work with what I have.  The problem is that I'm trying to edit, validate and update behind the scenes by allowing the user to make changes directly to the grid.

I usually code the data adpater, dataset, and connection string rather than from the toolbox and using an xsd and I usually do not update directly to the datagridview.  I was trying to get straight when to validate, when to update,  whether I have to code to handle if user changes a key column?

If the code is not available just answer those questions and I'll go from there.
>when to validate
In the CellValidating event of the grid

>when to update
Your decision but best practice is to provide a "Save Changes" button.

>whether I have to code to handle if user changes a key column
Yes. TableAdapter uses the key column in the Where clause of its Update and Delete statements so if the value of key column changes, things get messy. Why not make it readonly?
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

I'm not sure I'm following 100%.... but what I usually do is load the DataTable into the DataGridView and let the user use the DataGridView to add, delete, and edit the rows of the table.  Then I have a separate "Update" button that the users pushes to start the update process.   That way you could edit many rows and just have one update going on to handle all of the actions.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garyinmiami2003Author Commented:

 Your solution is closer to what I'm looking for and you will get most of the points.  Just answer this:

I plan to run in disconnected mode but will update the database row by row.  When user leaves a row, I plan to update and disconnect
What is an easy way of detecting a key change?

What's different in Graye's solution which you were looking for?
You can write an event handler for when a user is attempting to change to another row.   Then you could query the DataGridView to see if anything has changed, and then perform an update to the backend database.
However, that's not really "in the spirit" of the disconnected model... where you pull from the database, edit the locally cached copy, and then post the changes back.   In essence... 1 Fill operation and 1 Update operation.  Your way is more, Fill, Update, Update, Update, etc...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.