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?

[Webinar] Streamline your web hosting managementRegister Today

grayeConnect With a Mentor Commented:
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.
CodeCruiserConnect With a Mentor Commented:
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.
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

>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?
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...
All Courses

From novice to tech pro — start learning today.