问题
Im going crazy with this. I did the ff:
- Create a datatable.
- Fill it from a SQL db thru SqlDataAdapter.
- Edit the datatable thru datagridview.
- Call the sqldataadapter.update but the changes are not persisted to the db.
A closer look at the datatable after editing, the row states were not updated even if i actually edited the datatable thru the datagridview but the edited DataRow(s) have changes in the item array. Really confusing.. Any ideas? Thanks.
回答1:
You need several things to make this work. If you drag the table from the Data Sources view you end up with a few different things on your GUI:
- a dataSet
- a bindingSource
- the TableAdapter
- a tableAdapterManager
- a BindingNavigator
With these in place you can look at the source code to see what hapens beind the scenes. You'll need the EndEdit (as Baldi said before) but you need a little more:
private void UpdateGridView()
{
// validate that data types corresponds to database table column
this.Validate();
// ends edit on the graph table
this.graphBindingSource.EndEdit();
// ends edit on the graph table
this.intervalBindingSource.EndEdit();
// connect to the database - and exceute changes
this.tableAdapterManager.UpdateAll(this.diagramDBDataSet);
}
Hopefully this gets you started. If you want to learn more - you can follow this .NET database slide show with the complementary database tutorial. Good luck!
回答2:
Do you use DataBinding? Calling EndEdit may help...!
来源:https://stackoverflow.com/questions/3226932/c-sharp-sqldataadapter-update