Spooky2010
Spooky2010

Reputation: 341

Datagridview write row edits directly to database

Using Winforms, vs2008.

I have a datagridview bound to a bindingsouorce attached to a dataset with a datatable.

I want to be able to edit a row in the datagridview, and when the edits for that row are finished i want to write the data immediate to the database table via a stored procedure. (note this is the way i have to do it, via a call to a stored procedure).

my problem is finding an event that is reliably triggered when a user has finished editing 1 or more cells in the row, as they need.

Rows are never added in this grid they are only ever edits of the existing rows, or deleting of the row.

I could write via monitoring the datagridview.rowleave event or i could use the binding source PositionChanged or the current item changed events.

The problem is that all these events only fire when one moves away from the current row being edited. This is a big problem when there is only 1 row of data on the grid or editing the very last row on the grid. Not one of these events gets fired untill you move away from the row.

What can i do ? How can i deal with a single row on the grid or editing the last row, or deleting a row.

Any advice appreciated.

thanks

Upvotes: 1

Views: 759

Answers (1)

David Hall
David Hall

Reputation: 33143

You don't make it 100% clear when you consider a row ready to be saved. You say that it is a problem that you only get certain events fired when one moves away from the current row, but I'm not sure when else you could consider a row to be finished with editing?

That said, the two events I would look at are the DataGridView RowValidated event and the CellEndEdit event.

RowValidated is fired when ever changes to a row have been validated - generally when the row is left but unlike some events like BindingSource PositionChanged this event fires even when the grid loses focus. (there is also the DataGridView Leave event but that is probably not quite right).

If you want to commit whenever data within a row is changed then the CellEndEdit event is probably best.

Upvotes: 1

Related Questions