views:

3268

answers:

2

I'm having trouble saving a new row in the DataGridView back to the database. Briefly,

1) I use a SqlDataAdapter to fill a DataTable in a DataSet.

2) The DataTable is the DataSource for a Grid View.

3) If the user clicks on the blank "new row" at the bottom of the DataGridView, I capture the new, empty row of in the RowEnter event and populate it with data. The data appears in the DataGridView.

4) When I call Update on the SqlDataAdapter, the new data disappears and is not saved to the database.

Between steps 3 & 4 I have tried DataGrid.EndEdit, DataSet.AcceptChanges, BindingContext [...].EndCurrentEdit and maybe some other things without success.

How do I propagate the change from the DataGridView back to the DataSet?

And, if I may be permitted a brief whine, why is it so hard to find a simple example of such an obvious thing?

A: 

I have success with the approach here:

http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx

I still think it's too hard to find the answers in the documentation, though.

SeaDrive
+1  A: 

According to this, AcceptChanges may be the problem.

To quote:

Hey,

You can't execute AcceptChanges before the update. AcceptChanges makes the RowState back to Unchanged (which the added rows should be Added RowState value and the updated rows should be Modified RowState value).

The Update statement should call AcceptChanges for you

Notice that I didn't find this anywhere in MSDN.

Would you buy a search engine from a company that requires Google to query its own documentation?

le dorfier